Javascript Firestore如何从Firestore文档获取密钥
使用下面的查询,我可以获取document和doc.data().name以获取键“name”的值。我想要那份文件中的所有钥匙。我怎么能得到这个Javascript Firestore如何从Firestore文档获取密钥,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,使用下面的查询,我可以获取document和doc.data().name以获取键“name”的值。我想要那份文件中的所有钥匙。我怎么能得到这个 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 al
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);
});
根据我的示例,您可以执行以下操作:
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);
});