简体   繁体   English

Firebase Cloud Firestore 查询找不到我的文档

[英]Firebase Cloud Firestore query not finding my document

Here's a picture of my data:这是我的数据图片: 在此处输入图片说明

I'm trying to get that document.我正在尝试获取该文件。 This works:这有效:

var docRef = db.collection('users').doc('jPDKwHyrFNXNTFI5qgOY');
docRef.get().then(function(doc) {
  if (doc.exists) {
    console.log("Document data:", doc.data());
  } else {
    console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

It returns:它返回:

在此处输入图片说明

Ie, if I know the document's key I can get the document.即,如果我知道文档的密钥,我就可以获得文档。

This doesn't work:这不起作用:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.exists) {
    console.log(querySnapshot.data);
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});

It just returns No such document!它只是返回No such document! What's wrong with my query?我的查询有什么问题?

The difference in your two requests is that in the first case you are retrieving one document which gives you a DocumentSnapshot which has the exists property and the data() method.您的两个请求的不同之处在于,在第一种情况下,您正在检索一个文档,该文档为您提供一个DocumentSnapshot ,该DocumentSnapshot具有exists属性和data()方法。

In the second case you do a query, which gives you a QuerySnapshot that has to be handled differently from a DocumentSnapshot .在第二种情况下,您执行查询,这会为您提供一个QuerySnapshot ,其处理方式与DocumentSnapshot不同。 Instead of a single document you get a list/collection of documents.您会得到一个文档列表/集合,而不是单个文档。 You can check if data has been retrieved using the empty or size properties, and then go through the results using the forEach method or going through the docs array:您可以使用emptysize属性检查是否已检索到数据,然后使用forEach方法或通过docs数组查看结果:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.size > 0) {
    // Contents of first document
    console.log(querySnapshot.docs[0].data());
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});

I had the same problem with my project.我的项目遇到了同样的问题。 First of all there have to be an object in your database that match with the .where('objectid',==, 'object.id') then it is possible to get your data to loop.首先,您的数据库中必须有一个与 .where('objectid',==, 'object.id') 匹配的对象,然后就有可能让您的数据循环。 Like querysnapshot.docs.forEach((d) => d.data());像 querysnapshot.docs.forEach((d) => d.data());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cloud FireStore:使用查询检索1个文档 - Cloud FireStore: Retrieve 1 document with query Firebase Function 查询和更新单个 Firestore 文档 - Firebase Function query and update a single Firestore document Cloud Firestore:查询找不到文档,但是文档存在 - Cloud Firestore: Query does not find a document but document exists 从firebase onCall云功能中读取Firestore中的文档 - Read document in Firestore from firebase onCall cloud function Cloud Firestore 停止在用于 firebase 身份验证的集合中创建文档 - Cloud firestore stopped creating document in the collection used with firebase authentication Firestore接管一分钟搞定里面的文档 Firebase Cloud Function - Firestore take over a minute to get a document inside Firebase Cloud Function 如何在 Firebase/Google Cloud Firestore 的集合中获取最新添加的文档? - How to fetch latest added document in a collection in Firebase / Google Cloud Firestore? Firebase Cloud Firestore,如何使用 javascript 仅接收 4 个文档? - Firebase Cloud Firestore, How to receive only 4 document using javascript? 如何使用 Firebase 云功能更新 Firestore 文档的值 - How to update value of a firestore document with a firebase cloud functions 是否可以获取Firebase Cloud Firestore数据库中可用的集合的文档ID? - Is it possible to get document id of collections available in Firebase Cloud Firestore database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM