我是网络新手(一切都是异步的)。我正在尝试在 Angular + Firebase 应用程序中完成两步过程:
- 查询 Firestore 集合以查找与过滤器匹配的文档的 ID(名称 == 'theName')。
- 然后使用 ID 更新文档。
我来自嵌入式世界,在那里我可以做这样的事情(应用程序的上下文 - 我正在尝试跟踪战斗机器人比赛的结果)。
// Update the winning robot's doc with results
// Find the ID of the robot we're looking for (ex. winningRobotName = "Some Name")
this.firestore.collection('robots', ref => ref.where('name', '==', winningRobotName)).snapshotChanges()
.subscribe(data => {
this.bots = data.map(e => {
return {
id: e.payload.doc.id,
name: e.payload.doc.data()['name'],
// other fields
} as Robot;
})
});
let robot = this.bots[0]; <--------- This doesn't work because I reach here before the above call returns.
// Update this robot with new fields
this.firestore.doc('robots/' + robot.id)
.update({
fightCount : robot.fightCount + 1,
winCount : robot.winCount + 1,
// other updates
});
在执行另一个命令之前如何等待一个订阅返回?你嵌套订阅吗?有什么我不知道的非常基本的东西吗?
谢谢。