如何根据ID获取文档下的所有数据 [英] How to get all the data underneath a document based on a ID

查看:48
本文介绍了如何根据ID获取文档下的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在学习Firestore并努力编写查询.我想定位giMXcFmLUxfCaCmyYo0TJFeEHBL2文档下的所有字段.我的查询看起来像这样:

currently i'm learning Firestore and struggling to write a query. I want to target all the fields under the giMXcFmLUxfCaCmyYo0TJFeEHBL2 document. My query is looking like this:

 if (userId) {
  firebase
    .firestore()
    .collection('userLists')
    .doc(userId)
    .get()
    .then(docRef => {
      console.log('DATA', docRef.data());
    })
    .catch(error => {
      console.log('Nothing here');
    });
 }

docRef.data()一直说它是未定义的..我在做什么错(对不起,可能是愚蠢的问题..).我的目标是获取所有对象,例如

docRef.data() is keep saying it is undefined.. what am i doing wrong (sorry for maybe dumb question..). My goal is to get all the objects e.g.

2gzrNnVjpKOGLz2lxT6n
  id: "2gzrNnVjpKOGLz2lxT6n"
  time: 31 January 2021 at 00:00:00 UTC+1

安全规则:

    rules_version = '2';
      service cloud.firestore {
        match /databases/{database}/documents {
          match /{document=**} {
            allow read, write: if false;
          }
    
          match /lists/{document=**} {
            allow read, write: if true;
          }
    
          match /users/{document=**} {
            allow read, write: if true;
          }
    
          match /userLists/{document=**} {
            allow read, write: if true;
          }
       }
    }

推荐答案

我可以查询您的Firestore数据库(因为我们可以看到您的项目ID),看来文档的确切ID是"giMXcFmLUxfCaCmyYo0TJFeEhBL2";而不是"giMXcFmLUxfCaCmyYo0TJFeEhBL2":第一个字符是空格.

I could query your Firestore database (since we can see your project ID) and it appears that the exact ID of your document is " giMXcFmLUxfCaCmyYo0TJFeEhBL2" and not "giMXcFmLUxfCaCmyYo0TJFeEhBL2": the first character is a space.

奇怪的是(并且引起误解)是Firestore控制台在集合中显示doc ID时没有显示空格(请参见下图中的红色矩形,这是我的一个项目中的示例),但是如果单击显示文档路径的区域,则可以看到该空间(请参见下图的蓝色矩形).

What is weird (and misleading) is that the Firestore console does not show the space when displaying the doc ID in the collection (see the red rectangle in the image below, which is an example in one of my projects), but if you click on the area where the document path is displayed, you can see the space (see the blue rectangle in the image below).

我可以通过查询集合来发现这一点:

I could discover that by querying the collection with:

  firebase
    .firestore()
    .collection('userLists')
    .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);
    });


(由于Firebase项目ID已发布,因此我建议您保护Firestore数据库的安全)


(I would suggest you secure your Firestore DB since the Firebase project ID is published)

这篇关于如何根据ID获取文档下的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆