简体   繁体   English

如何从 firebase 中的引用文档中获取数据?

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

In my data model i reference a user document from a "card" document.在我的数据 model 中,我引用了“卡片”文档中的用户文档。

1个

I am struggeling to get the data of the user document using react.我正在努力使用 React 获取用户文档的数据。

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

This code logs an object without any data.此代码记录了一个没有任何数据的 object。 Here is the object "gu":这是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

A reference field type is "just" a reference. reference字段类型“只是”一个引用。 You can't get directly any data from it beside the path.除了路径之外,您无法直接从中获取任何数据。 The log you see is the same log that you would see if you would log any other reference in your code.您看到的日志与您在代码中记录任何其他引用时看到的日志相同。

The only way to get data from that ref it so call get() ony it:从该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.

相关问题 在 Node JS 中从 Firebase Firestore 获取文档数据 - Get document data from Firebase Firestore in Node JS 使用“where”查询从 firebase 文档中获取数据而没有特定的 doc() - get data from firebase document with "where" query without specific doc() 来自 Firebase 文档的数据总和 - Sum of data from a firebase document 如何通过 flutter 中的文档从 firebase 存储中检索数据? - How do i retrieve data from firebase store by document in flutter? 如何从 Firebase Firestore 集合中指定文档的特定字段获取数据并将其显示到我的 UI? - How do I get data from a specific field of the specified document inside a Firebase Firestore collection and display it to my UI? 如何防止恶意用户将数据推送到 Firebase 中的文档中? - How to prevent a malicious user from pushing data into a document in Firebase? 如何从 firebase 获取最新的更新数据? - How to get the latest updated data from firebase? 如何从flutter中获取firebase的数据 - how to get data from firebase in flutter 如何从 Firebase 数据库中获取孩子的数据? - How to get data from a child in Firebase Database? 如何使用 Kotlin 从 Firebase 获取特定数据 - How to get specific data from Firebase with Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM