繁体   English   中英

Firestore 获取已查询文档的路径

[英]Firestore get path of already queried document

我正在尝试获取 Firebase Firestore 中查询文档的路径。 这是我的代码:

const location = await db.doc('/locations/US/regions/IOWA').get()
console.log(location.path)

但是location.path返回未定义。

location.id工作并返回 id。

我将其用作资源: https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#path

当您在DocumentReference上调用get()时,您会返回一个DocumentSnapshot ,它没有path属性。

您可能正在寻找location.ref.path

我知道这是旧的和已回答的问题,这就是我从查询的文档中获取文档参考的方式

d.get()
.then(td=>{
   let taskDep = td.data()
   taskDep.id = td.id
   taskDep.ref = td.ref

})

DocumentSnapshot 的 API 文档说可以在它的 reference 属性中找到文档的引用。 所以你会想要使用这个:doc.reference。

当前接受的答案似乎已过时,因为DocumentSnapshot现在确实存储了路径。

每个DocumentSnapshot都有一个在ref属性下找到的DocumentReference ( https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentSnapshot#ref )

DocumentSnapshot中,您可以在path属性下找到路径的字符串表示形式( https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentReference#path

简而言之,您可以简单地使用doc.ref.path来获取路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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