最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firestore get path of already queried document - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a comment  | 

4 Answers 4

Reset to default 11

When 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

})
发布评论

评论列表(0)

  1. 暂无评论