【发布时间】:2019-09-09 08:47:56
【问题描述】:
我有一个 Firestore 文档,它有两个属性,它们都是 Firestore 数据库中其他位置的不同 <Game>{} 对象的 Id 数组。
我有什么方法可以创建一个 observable 流式传输一个对象,该对象包含两个单独的项目数组,这些 id 代表的项目?
我认为没有办法只拥有一个单一的对象流,因为(一旦我订阅)我不知道哪个项目属于哪个数组,因为数组大小不是恒定的。
我是 observables 和 rxjs 的新手,并且经常阅读有关不同运算符的信息,但我有点卡在这里,如果找不到解决方案,我正在考虑重组我的数据。
现在,我的服务类中有类似的东西,但是当我订阅该功能时,它并没有像我预期的那样工作。
getPlayerGames(uid: string) {
return this._db.doc<PlayerGameSession>(`users/${uid}/mygamesession/games`).valueChanges().pipe(
map(gameObj => {
let combined1;
let combined2;
if (gameObj.prop1) {
const prop1Streams = gameObj.prop1.map(
gameId => this._db.doc<Game>(`games/${gameId}`).valueChanges()
);
combined1 = combineLatest(...prop1Streams);
}
if (gameObj.prop2) {
const prop2Streams = gameObj.prop2.map(
gameId => this._db.doc<Game>(`games/${gameId}`).valueChanges()
);
combined2 = combineLatest(...prop2Streams);
}
return combineLatest(combined1, combined2);
}),
switchMap(([c1, c2]) => of({ c1: c1, c2: c2 }))
);
}
【问题讨论】:
标签: angular rxjs google-cloud-firestore observable