【问题标题】:Firestore Snapshot Read only last documentFirestore 快照 只读最后一个文档
【发布时间】:2020-06-26 22:42:22
【问题描述】:
fire.firestore().collection('Customer').get()
  .then(data=>{
    data.docs.forEach(doc=>{
      let db = fire.firestore().collection(`Customer`)
      db.where("updated", ">=", 0).limit(100).onSnapshot(async doc=>{
        try {
          await doc.docs.map(each=>{
            setDatas([...datas, {...each.data()}])
          })
        }
      })
  })

我正在尝试将数组中的对象附加到 Firestore 中进行查询。 但是,不知何故,它只读取最后一个文档。 如果您可以在不使用数组的情况下存储状态,请帮助我。

【问题讨论】:

  • 你试图附加什么“对象”?
  • 我想将对象从 ...each.data() 附加到状态数组

标签: reactjs google-cloud-firestore


【解决方案1】:

检查您的代码并运行此data 作为示例,我注意到您的代码正在获取所有结果,而不仅仅是最后一个结果,因此可能还有其他内容。此外,这似乎是一个不必要的双重查询。

相反,您可以使用 .. where("updated", ">=", 0).limit(100) .. 而不是查询中的查询来查询您可能想要获取的所有文档。

您可以使用here 中的示例代码使事情变得更简单:

const admin = require('firebase-admin');
admin.initializeApp();
let db = admin.firestore();

const citiesRef = db.collection('Customer');
const snapshot = await citiesRef.where('updated', '>=', 100).limit(100).get();
if (snapshot.empty) {
console.log('No matching documents.');
return;
}  

snapshot.forEach(doc => {
    //Your code here to add to your array
});

此代码适用于NodeJS,但您可以将其调整为ReactJS

【讨论】:

    猜你喜欢
    • 2020-01-05
    • 2022-01-25
    • 2021-09-27
    • 2018-03-19
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多