admin管理员组文章数量:1205763
I am trying to get the path of a queried document in Firebase Firestore. This is my code:
const location = await db.doc('/locations/US/regions/IOWA').get()
console.log(location.path)
However location.path
returns undefined.
location.id
works and returns the id.
I have used this as a resource: .firestore.DocumentReference#path
I am trying to get the path of a queried document in Firebase Firestore. This is my code:
const location = await db.doc('/locations/US/regions/IOWA').get()
console.log(location.path)
However location.path
returns undefined.
location.id
works and returns the id.
I have used this as a resource: https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#path
Share Improve this question asked Sep 3, 2019 at 3:43 Leonard NiehausLeonard Niehaus 5306 silver badges16 bronze badges4 Answers
Reset to default 11When you call get()
on a DocumentReference
, you get back a DocumentSnapshot
, which does not have a path
property.
You're probably looking for location.ref.path
.
The currently accepted answer appears to be outdated, as aDocumentSnapshot
does now store the path.
Every DocumentSnapshot
has a DocumentReference
found under the ref
property (https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentSnapshot#ref)
In the DocumentSnapshot
, you can then find a string representation of the path under the path
property` (https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentReference#path)
In short, you can simply use doc.ref.path
to get the path.
The API documentation for DocumentSnapshot says that the reference of the document can be found in its reference property. So you will want to use this: doc.reference.
I know it is old and answered question, this is how I get the document reference from queried document
d.get()
.then(td=>{
let taskDep = td.data()
taskDep.id = td.id
taskDep.ref = td.ref
})
本文标签: javascriptFirestore get path of already queried documentStack Overflow
版权声明:本文标题:javascript - Firestore get path of already queried document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738709280a2108097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论