I have created a collection name ("users") and inside it i have created subCollection("innerGuides"). I know how to retrieve data from the main collection ("users"), but i want to know how can i get the ID of my subCollection("innerGuides").
db.collection("users").doc(user.uid).collection("innerGuides").doc("BGP9O0Rh2ThiL40tXOVG").get().then(doc => {
console.log(user.uid);
console.log(doc.data().content);
}).catch(error => {
console.log(error);
})
as you can see i can retrieve my main collection UID through "user.uid" but for my subCollection how can i retrieve its "uid". I have typed the subCollection uid, but how to retrieve it dynamically.
To get the id of a DocumentSnapshot is, from your example:
db.collection("users").doc(user.uid).collection("innerGuides").doc("BGP9O0Rh2ThiL40tXOVG").get().then(doc => {
console.log(doc.id); // <-- here
console.log(doc.data().content);
}).catch(error => {
console.log(error);
})
You can also use doc.exists
to see if the document your get()
actually exists.
To see your document, with id, something like:
console.log({
id: doc.id,
...doc.data(),
});
The id of the sub-collection something like: users/the-user-id/innerGuides/BGP9O0Rh2ThiL40tXOVG
. There is no way to only return BGP9O0Rh2ThiL40tXOVG
, as by itself it isn't a complete reference. If you need that value parse it out of the id. JavaScript has many ways to do that. One off the top of my head is split():
const subId = id.split('/')[3]
Probably best to validate the array first, etc., but it will get you the id when successful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.