Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何基于ID获取文档下的所有数据_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 如何基于ID获取文档下的所有数据

Javascript 如何基于ID获取文档下的所有数据,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,目前我正在学习Firestore并努力编写查询。我想针对GIMxCFMLEXFCACMYO0TJFEEHBL2文档下的所有字段。我的查询如下所示: if (userId) { firebase .firestore() .collection('userLists') .doc(userId) .get() .then(docRef => { console.log('DATA', docRef.data()); })

目前我正在学习Firestore并努力编写查询。我想针对GIMxCFMLEXFCACMYO0TJFEEHBL2文档下的所有字段。我的查询如下所示:

 if (userId) {
  firebase
    .firestore()
    .collection('userLists')
    .doc(userId)
    .get()
    .then(docRef => {
      console.log('DATA', docRef.data());
    })
    .catch(error => {
      console.log('Nothing here');
    });
 }
docRef.data()一直在说它未定义。。我做错了什么(对不起,可能是愚蠢的问题…)。 我的目标是得到所有的对象,例如

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似乎是“gimxcfluxfcacmyyotjfeehbl2”,而不是“gimxcfluxfcacmyyotjfeehbl2”:第一个字符是空格

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

通过查询集合,我可以发现:

  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);
    });


(我建议您保护您的Firestore数据库,因为Firebase项目ID已发布)

我可以查询您的Firestore数据库(因为我们可以看到您的项目ID),您的文档的确切ID似乎是“gimxcfluxfcacmyyo0tjfeehbl2”,而不是“gimxcfluxfcacmyyo0tjfeehbl2”:第一个字符是空格

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

通过查询集合,我可以发现:

  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);
    });


(我建议您保护Firestore数据库,因为Firebase项目ID已发布)

您可以检查
userId
是否具有正确的值,即
console.log(userId)
打印
giMXc…
?您的代码似乎正常(注意,
docRef
是一个
DocumentSnapshot
,不是
DocumentReference
,但这只是变量命名的一个细节)感谢您的回复。控制台显示:userId giMXcFmLUxfCaCmyYo0TJFeEhBL2,因此userId变量应该相同。可能存在阻止读取文档的安全规则。您应该使用
exists
属性检查您是否可以读取文档,如下所示:@RenaudTarnec I添加了exists检查,然后控制台显示:No such文档!有任何阻止读取文档的安全规则吗?您是否100%确定使用了正确的Firebase项目?是否可以检查
userId
是否具有正确的值,即
console.log(userId)
打印
giMXc…
?您的代码似乎正常(注意,
docRef
是一个
DocumentSnapshot
,不是
DocumentReference
,但这只是变量命名的一个细节)感谢您的回复。控制台显示:userId giMXcFmLUxfCaCmyYo0TJFeEhBL2,因此userId变量应该相同。可能存在阻止读取文档的安全规则。您应该使用
exists
属性检查您是否可以读取文档,如下所示:@RenaudTarnec I添加了exists检查,然后控制台显示:No such文档!是否存在阻止读取文档的安全规则?是否100%确定使用了正确的Firebase项目?