Reputation: 302
Using below query, I can get the document and doc.data().name to get the value for the key '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);
});
Upvotes: 5
Views: 12736
Reputation: 11
In firebase, If you want to access the value of a key in a document inside your collection. Let's say key "name" or "email". Use the code below:
searchSnapshot.docs[index]["name"].data()
searchSnapshot.docs[index]["email"].data()
Note: I used search box here, you might have a different plugin to implement.
Upvotes: 0
Reputation: 598728
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);
});
Upvotes: 9