簡體   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