admin管理员组文章数量:1202372
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:
I.e., 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!
What's wrong with my query?
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:
I.e., 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!
What's wrong with my query?
- Please tag with the languages used for code. This is JavaScript. – tadman Commented Oct 11, 2017 at 16:13
2 Answers
Reset to default 25The 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.
In the second case you do a query, which gives you a QuerySnapshot
that has to be handled differently from a 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:
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. Like querysnapshot.docs.forEach((d) => d.data());
本文标签: javascriptFirebase Cloud Firestore query not finding my documentStack Overflow
版权声明:本文标题:javascript - Firebase Cloud Firestore query not finding my document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738564060a2099798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论