[英]How to get data from a referenced document in firebase?
In my data model i reference a user document from a "card" document.在我的数据 model 中,我引用了“卡片”文档中的用户文档。
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.