【问题标题】:How do I add a .then to a firebase getDocs()如何将 .then 添加到 firebase getDocs()
【发布时间】:2022-11-30 09:23:21
【问题描述】:

从 firebase 检索所有文档后,我需要执行一个操作,但 getDocs 不允许 .then。

有一个帖子有一个例子,但它比我认为我需要的要多,而且我仍然不明白如何实施该解决方案。

这是代码:Using getDoc().then() inside of a loop Firebase

    const q = this.itineraryService.findData(filters);

    const querySnapshot = await getDocs(q);
    querySnapshot.forEach((doc) => {
      this.array.push({ ...doc.data(), id: doc.id});
    });

我希望能够只添加一个 .then 到最后一个 ) 但它不允许这样做。那么如何才能等到拿到所有的单据之后再进行下一步的操作呢。

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    如果你想使用then而不是await,那就是:

    getDocs(q).then((querySnapshot) => {
      querySnapshot.forEach((doc) => {
        this.array.push({ ...doc.data(), id: doc.id});
      });
    });
    

    这是有效的,因为 getDocs function 返回 Promise<QuerySnapshot>

    【讨论】:

    • 我想等到 querySnapshot 完成检索所有文档后。
    • 你已经用await做了。您不能可靠地组合 awaitthen。这听起来像XY problem。您共享的代码有什么问题让您认为您同时需要awaitthen?有错误信息吗?
    猜你喜欢
    • 2022-07-27
    • 1970-01-01
    • 2019-04-02
    • 2021-07-04
    • 2017-04-28
    • 2022-01-15
    • 2020-08-14
    • 2021-12-25
    • 2021-11-23
    相关资源
    最近更新 更多