[英]Firestore how to get keys from firestore document
Using below query, I can get the document and doc.data().name to get the value for the key 'name'. 使用下面的查询,我可以获取文档和doc.data()。name来获取键'name'的值。 I would like to get all the keys in that document.
我想获得该文档中的所有密钥。 How can I get that?
我怎么能得到它?
var docRef = db.collection("cities").doc("SF");
docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
//**I want to get all keys in the document here.**
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
Based on my example from this answer , you can do: 基于我在这个答案中的例子,你可以做到:
docRef.get().then(function(doc) {
if (doc.exists) {
let json = doc.data();
console.log("Document data:", json);
console.log("Document keys:", Object.keys(json));
Object.keys(json).forEach((name) => {
console.log(name, json[name]);
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.