[英]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:您可以使用empty
或size
属性检查是否已检索到数据,然后使用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.