javascript - firebase如何获取过滤后的文档

标签 javascript firebase google-cloud-firestore

我有一个集合lots,其中包含由firebase生成的许多文档,这些文档中包含采用这种形式的对象

//lot Document
   {
    attr1: {
        subattr1 : "some value",
        ...
    },
    attr2: {
        subattr1 : "some value",
        ...
    },
    user: {
        id : "some uid value xAsDFadfaWRDAFd",
        ...
    }

所以在我的 firebase 中我有

//my collection
         xAsDFadfaWRDAFd-loot1
         xAsDFadfaWRDAFd-loot2
         xAsDFadfaWRDAFd-loot3

现在我只想能够通过用户 ID 属性过滤集合。

我已经尝试过

firebase.collection('lots').where('user.id','==',uid).get().then(do Something if i find them);

但它找不到任何内容,如果我采用 .where('user.id','==',uid) 它会从集合中检索所有文档,除了将来我可能想按文档中的另一个属性进行过滤,我该怎么做?

最佳答案

您在 then() 子句中未显示的代码可能是错误的。我在控制台中设置了数据并成功运行了代码:

Image of the Firebase Console with code on the left, and data on the right

代码如下:

db.collection("lots").where("user.id","==","ABCDE").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      // doc.data() is never undefined for query doc snapshots
      console.log(doc.id, " => ", doc.data());
    });
  })
  .catch(function(error) {
    console.log("Error getting documents: ", error);
  });

如果 then() 子句中的代码实际上是正确的,则考虑检查 userid 的大小写是否相同,并且uid 包含您期望的数据(一种方法是对您从控制台中的文档复制/粘贴的值进行硬编码。

关于javascript - firebase如何获取过滤后的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183893/

相关文章:

javascript - 自定义重试功能

javascript - 在 jQuery 中解析多部分/混合响应

node.js - admin.messaging 不是一个函数

node.js - Cloud Functions 中的 Cloud Firestore

firebase - 添加数据读取限制 firebase firestore

javascript - 按日期升序排序 sequelize 查询的问题

javascript - div滚动时如何调整宽度?

node.js - 如何将云函数部署到.firebaserc 中的不同项目?

firebase - 无法检查 DocumentSnapshot 上的 containsKey

firebase - 使用 Firestore 时在 Cookie 中存储 Firebase Auth UID - 这安全吗?