簡體   English   中英

如何從 firebase 中的引用文檔中獲取數據?

[英]How to get data from a referenced document in firebase?

在我的數據 model 中,我引用了“卡片”文檔中的用戶文檔。

1個

我正在努力使用 React 獲取用戶文檔的數據。

onSnapshot(doc(firestore, "cards", cardId), (doc) => {
    const userDocument = doc.data().uid;
    console.log(userDocument);
});

此代碼記錄了一個沒有任何數據的 object。 這是object“gu”:

gu:
    converter: null
    firestore: Su {type: "firestore", _persistenceKey: "[DEFAULT]", _settings: wu, _settingsFrozen: true, _app: FirebaseAppImpl, …}
    type: "document"
    _key: wt {path: Z}
    id: (...)
    parent: (...)
    path: (...)
    _path: (...)
    [[Prototype]]: Object

reference字段類型“只是”一個引用。 除了路徑之外,您無法直接從中獲取任何數據。 您看到的日志與您在代碼中記錄任何其他引用時看到的日志相同。

從該ref獲取數據的唯一方法是調用get() only:

onSnapshot(doc(firestore, "cards", cardId), async (doc) => {
    const userDocument = doc.data().uid;
    userDocument.get().then(r=>{
      console.log(r.data());
    })
    
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM