简体   繁体   English

Firestore 获取已查询文档的路径

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM