【问题标题】:how to use where in cloud firestore如何在 Cloud Firestore 中使用 where
【发布时间】:2018-07-02 07:31:31
【问题描述】:

我在 firebase 功能中使用云 Firestore。我在查询中使用“where”时遇到问题,因为我不知道确切的语法。如果我错了,请纠正我。

self.dbManager.collection('test').where('entry_date', '==','2018-06-29').where('entry_status','==','active').get().then( async doc => {
                console.log("doc--------->",doc);
                console.log("doc--------->",doc.data());
                conv.data.userQuesData  =   doc.data()
});

当我使用“doc.data()”时,我遇到了错误。我们有上述条件的数据

TypeError: doc.data is not a function
    at MYservice.<anonymous> (/user_code/services/myService.js:99:62)
    at step (/user_code/services/myService.js:32:23)
    at Object.next (/user_code/services/myService.js:13:53)
    at /user_code/services/myService.js:7:71
    at __awaiter (/user_code/services/myService.js:3:12)
    at /user_code/services/daoService.js:94:168
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    执行查询时,get() 返回一个包含 QuerySnapshot 对象的承诺。

    在您的代码中,您将该 QuerySnapshot 分配给一个名为 doc 的变量,然后显然假设它是一个 DocumentSnapshot 类型的对象,这是一个失败。

    您需要调整代码以处理 QuerySnapshot 而不是 DocumentSnapshot。请参阅QuerySnapshot 文档以了解从针对集合的查询中返回的内容。特别要注意,来自查询的文档匹配列表可以在 docs 属性中作为 QueryDocumentSnapshot 对象查看。

    【讨论】:

      【解决方案2】:

      当您对集合执行查询时,可能有多个文档与条件匹配。出于这个原因,查询返回一个QuerySnapshot,其中包含匹配的文档。您需要遍历 QuerySnapshot 来获取各个文档:

      self.dbManager.collection('test').where('entry_date', '==','2018-06-29').where('entry_status','==','active').get().then( async querySnapshot => {
          querySnapshot.forEach(doc => {
              console.log("doc--------->",doc.data());
          });
      });
      

      请注意,documentation on getting multiple documents 很好地涵盖了这一点,我推荐它,因为查询功能建立在此之上。

      【讨论】:

        猜你喜欢
        • 2018-06-10
        • 1970-01-01
        • 2021-01-07
        • 2018-08-19
        • 2019-12-12
        • 2019-12-01
        • 1970-01-01
        • 2021-03-29
        • 1970-01-01
        相关资源
        最近更新 更多