我是网络新手(所有内容都是异步的)。我正在尝试在Angular + Firebase应用程序中完成两步过程:
我来自嵌入式世界,在这里我可以做类似的事情(在应用程序的背景下-我试图跟踪战斗机器人竞赛的结果)。
// 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
});
在执行另一命令之前,如何等待一个订阅返回?您是否嵌套订阅?有什么我真的不了解的基本知识吗?
谢谢。
答案 0 :(得分:0)
我认为AngularFire不会在这里为您提供帮助,而是直接在常规JavaScript SDK上执行此操作:
ref.where('name', '==', winningRobotName))
.get()
.then((snapshots) => {
snapshots.forEach((doc) =>
doc.ref.update({
id: doc.id,
name: doc.data().name
})
})
})
我不确定name: doc.data().name
应该做什么(因为这是一个身份操作,提供与输入相同的结果),但是请留意以防万一。