Reputation: 7724
My Cloud Firestore database looks like this:
users
|
|----id----(name,age)
|----id----(name,age)
|----id----(name,age)
|----id----(name,age)
|----id----(name,age)
...
I'm writing a query through all the elements:
db.collection('users').get()
.then(
(results) => {
results.forEach((doc) => {
//how to get the id that represents this doc?
});
response.json();
}
)
.catch(function (error) {
console.error("Error getting user: ", error);
response.send(error)
});
My question is simple: the variable doc represents an id (that contains a collection with name and age). How can I get this id? can I retrieve it from doc somehow?
Upvotes: 0
Views: 150
Reputation: 598847
That's actually quite simple. The doc
variable is actually DocumentSnapshot
, and you get its ID from its id
property. So:
db.collection('users').get()
.then(
(results) => {
results.forEach((doc) => {
console.log(doc.id);
});
}
)
.catch(function (error) {
console.error("Error getting user: ", error);
response.send(error)
});
Upvotes: 1