gpt4 book ai didi

javascript - Firebase - 使用附加数据填充属性/数组

转载 作者:数据小太阳 更新时间:2023-10-29 05:37:20 25 4
gpt4 key购买 nike

下面的可观察对象创建了一个事件对象数组。

eventsRef: AngularFireList<any>;
events: Observable<any>;

this.eventsRef = db.list('events');

this.events = this.eventsRef.snapshotChanges().map(changes => {

return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});

我需要从其他数据库列表向 this.events 添加额外的数据。所以我需要每个事件对象都包含 guest 人数和数据 eventsFilters。我不知道该怎么做。这是我到目前为止所拥有的:

this.events = this.eventsRef.snapshotChanges().map(changes => {

changes.map(data => {
console.log(data.payload.key)
this.db.object(`/eventsFilters/${data.payload.key}`)
.valueChanges()
.subscribe(data => {
console.log(data) //event filters
})
})

changes.map(data => {
console.log(data.payload.key)
this.db.object(`/eventsGuests/${data.payload.key}`)
.valueChanges()
.subscribe(data => {
let guestCount = Object.keys(data).length;
console.log(guestCount)
this.guestCount = guestCount; //guest count
})
})
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});

编辑--------

我使用 combineLatest 走到了这一步,但我仍然不确定如何对每个事件数据进行分组。

this.eventsRef.snapshotChanges()
.switchMap(
(changes) => {
let userQueries: Observable<any>[] = [];
let lists: Array<string> = ['eventsFilters', 'eventsGuests'];

changes.map(data => {
for (let list of lists) {
userQueries.push(this.db.object(`/${list}/${data.payload.key}`).valueChanges());
}
})

userQueries.push(this.eventsRef.snapshotChanges());

return Observable.combineLatest(userQueries);
})
.subscribe((d) => {
console.log(d)
});

console.log(d) 输出如下内容:

[
{}, //object with data from eventsFilters for first event
{}, //object with data from eventsGuests for first event
{}, //object with data from eventsFilters for second event
{}, //object with data from eventsGuests for second event
...
[{},{} ...] //array with all events
]

最佳答案

下面是一个结合了 3 个 observable 的例子:

combinedData$ = combineLatest( entityList$, settings$, currentUser$).pipe(
map(([entityList, pageSetting, currentUser]) => {
//entityList, pageSettingand currentUser holds the last value emitted on each observables.

if (!pageSetting.ShowAllOrganisation) {
//If not showing all organisation, then we have to filter it
retVal = entityList.filter(entity=> entity.organisationId === currentUser.organisationId);
}

return retVal;
})
);

CombineLatest 将返回一个新的可观察对象。如果 3 个 observable 中的一个发出一个新值,则 combineLatest 将被触发并发出一个新值。有关 combineLatest 如何工作的更多信息,请访问 official documentation

关于javascript - Firebase - 使用附加数据填充属性/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com