您如何等待Firebase的数据?
我想等待数据返回,然后在AngularFire中调用我的下一个功能。
这是我当前的代码...我想替换设置超时...我知道这不是这样做的最好方法。
function(){
this.db.collection('myCollection').doc('myDoc').valueChanges().subscribe(res => {
this.myData = res
})
setTimeout(() => {
this.myFunction(this.myData)
}, 1500);
}
I want to wait for data to return before calling my next function in angularfire.
Here is my current code... I want to replace set timeout... I know this isnt the best way of doing this.
function(){
this.db.collection('myCollection').doc('myDoc').valueChanges().subscribe(res => {
this.myData = res
})
setTimeout(() => {
this.myFunction(this.myData)
}, 1500);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
等待
在这里没有意义。等待
是管理承诺的工具。反过来,承诺是等待一次发生一次的工具。valuechanges
发生每次价值变化,而不仅仅是一次。如果您要把它包裹在承诺中,那么它将在第一次更改值时起作用,但是下次下一次会失败。
您应该使用
get
而不是或在订阅功能中进行工作,以便完成每个工作时间变化。It doesn't make sense to
await
here.await
is a tool to manage Promises. Promises, in turn, are a tool to wait for something to happen once.valueChanges
happen every time the value changes, not just once.If you were to wrap this in a promise, then it would work the first time the value changed, but then fail the next time it did.
You should either use
get
instead or do the work in the subscription function so it gets done each time the value changes.这应该解决问题
This should do the trick