简体   繁体   English

如何获取 Firestore 文档中的嵌套地图字段

[英]How to fetch nested map fields in a Firestore Document

I've been using Firestore in Firebase and for now i've been able to extract the whole document but I can't extract one of the variables (for example, latitude):我一直在 Firebase 中使用 Firestore,现在我已经能够提取整个文档,但我无法提取其中一个变量(例如纬度): Firestore 文档截图

文档输出示例

And here is my code that works (JavaScript with React Native and Expo):这是我的工作代码(JavaScript with React Native 和 Expo):

let cityRef = db.collection('pacientes').doc('paciente-test');
let getDoc = cityRef.get()
.then(doc => {
if (!doc.exists) {
  console.log('No such document!');
} else {
  console.log(doc.id, " => ", doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
})

I tried many things with doc.data(), like doc.data().latitude (with an undefined output) but whenever I change it, it doesn't compile.我用 doc.data() 尝试了很多东西,比如 doc.data().latitude(带有未定义的输出),但是每当我更改它时,它都不会编译。 I've seen other solutions around but they don't work for me for some reason.我已经看到了其他解决方案,但由于某种原因它们对我不起作用。 If you think I've missed any info that could be more helpful let me know如果您认为我错过了任何可能更有帮助的信息,请告诉我

Your document contains a single field called "safeArea" which has nested properties.您的文档包含一个名为“safeArea”的字段,该字段具有嵌套属性。 You have to reference those nested properties as object properties in the "safeArea" object, like this:您必须将这些嵌套属性引用为“safeArea”对象中的对象属性,如下所示:

doc.data().safeArea.latitude

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM