[英]Firestore get path of already queried document
I am trying to get the path of a queried document in Firebase Firestore.我正在尝试获取 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.path
返回未定义。
location.id
works and returns the id. location.id
工作并返回 id。
I have used this as a resource: https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#path我将其用作资源: https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#path
When you call get()
on a DocumentReference
, you get back a DocumentSnapshot
, which does not have a path
property.当您在
DocumentReference
上调用get()
时,您会返回一个DocumentSnapshot
,它没有path
属性。
You're probably looking for location.ref.path
.您可能正在寻找
location.ref.path
。
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
})
The API documentation for DocumentSnapshot says that the reference of the document can be found in its reference property. DocumentSnapshot 的 API 文档说可以在它的 reference 属性中找到文档的引用。 So you will want to use this: doc.reference.
所以你会想要使用这个:doc.reference。
The currently accepted answer appears to be outdated, as a DocumentSnapshot
does now store the path.当前接受的答案似乎已过时,因为
DocumentSnapshot
现在确实存储了路径。
Every DocumentSnapshot
has a DocumentReference
found under the ref
property ( https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentSnapshot#ref )每个
DocumentSnapshot
都有一个在ref
属性下找到的DocumentReference
( 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 )在
DocumentSnapshot
中,您可以在path
属性下找到路径的字符串表示形式( 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.简而言之,您可以简单地使用
doc.ref.path
来获取路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.