您如何等待Firebase的数据?

发布于 2025-02-13 23:17:15 字数 325 浏览 2 评论 0原文

我想等待数据返回,然后在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懵少女 2025-02-20 23:17:16

等待在这里没有意义。

等待是管理承诺的工具。反过来,承诺是等待一次发生一次的工具。

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.

乖乖公主 2025-02-20 23:17:16

这应该解决问题

const myData = (
  await this.db.collection('myCollection').doc('myDoc').get().toPromise()
).data()

This should do the trick

const myData = (
  await this.db.collection('myCollection').doc('myDoc').get().toPromise()
).data()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文