简体   繁体   English

Firestore/Javascript:如何从子文档的地图字段中获取数据?

[英]Firestore/Javascript: How to get a data from map field of a sub-document?

I want to get a specific data from a map of my sub-document in firestore and display it my web application page.我想从我在 firestore 中的子文档的地图中获取特定数据并将其显示在我的 Web 应用程序页面上。 I don't really know how to call the data during the var.textContent = doc.data().我真的不知道如何在var.textContent = doc.data().期间调用数据var.textContent = doc.data(). part...部分...

Note: stages is a field map注:stages 为现场图

Image to help with my question:帮助解决我的问题的图片:

在此处输入图片说明

So far this is what I have:到目前为止,这就是我所拥有的:

<script>
firebase.auth().onAuthStateChanged(user => {
 if(user){
  this.userId = user.uid;
 } //stores the user id in variable

const fitbitremSleep= document.querySelector('#fitbitSleepListRemSleep');

function renderFitbitSleep(doc){

 let li = document.createElement('li');
 li.setAttribute('data-id', doc.id);

 let remSleep = document.createElement('span');
 let lbremSleep = document.createElement('span');

 remSleep.textContent = doc.data();
 lbremSleep.textContent = "Minutes of REMSleep:  ";  

 li.appendChild(lbremSleep);
 li.appendChild(remSleep);

 fitbitremSleep.appendChild(li);
 }

 let userRef1 = 
 firebase.firestore().collection("users").doc(userId).collection("fitbit_sleep").orderBy("dateAdded", 
 "desc").limit(1);
 return userRef1.get()
 .then(function(querySnapshot) {
  querySnapshot.forEach(function(doc) {
   console.log(doc.id, " => ", doc.data());
   renderFitbitSleep(doc);
   });
  })
  .catch(function(error) {
   console.log("Error getting documents: ", error);
    });
   });
});
</script>

EDIT编辑

To clarify my question even more is it possible to get a content of field map in firestore using doc.data() ?为了进一步澄清我的问题,是否可以使用doc.data()在 firestore 中获取字段映射的内容? Because I've been using doc.data() to get fields of a sub-document.因为我一直在使用doc.data()来获取子文档的字段。

EXAMPLE例子

Because I've been using doc.data() to get fields of a sub-document.因为我一直在使用doc.data()来获取子文档的字段。

My code to set a non-map field:我设置非地图字段的代码:

minutesAsleep.textContent = doc.data().minutesAsleep;

note that: minutesAsleep is non-map field within the sub-document already and can be easily pulled out from the database using the aforementioned code.请注意: minutesAsleep已经是子文档中的非地图字段,可以使用上述代码轻松从数据库中拉出。

Field minutesAsleep in the database:数据库中的字段minutesAsleep 在此处输入图片说明

EXPECTED OUTPUT OF THE EXAMPLE:示例的预期输出: 在此处输入图片说明

After experimenting and tinkering with the code:在试验和修改代码之后:

 remSleep.textContent = doc.data();
 lbremSleep.textContent = "Minutes of REMSleep:  ";

Replace with this:替换为:

 remSleep.textContent = doc.data().stages.remSleep;
 lbremSleep.textContent = "Minutes of REMSleep:  ";

to explain further:进一步解释:

doc.data().

gets the docs data获取文档数据

then,然后,

doc.data().stages

gets the docs data under map field stages获取地图字段阶段下的文档数据

finally,最后,

doc.data().stages.remSleep;

gets the docs data under map field stages and gets the data of remSleep under that map.获取map field stage下的docs数据,获取该map下remSleep的数据。

I hope this could help anyone especially those who are developing using javascript and firebase :)我希望这可以帮助任何人,尤其是那些使用 javascript 和 firebase 进行开发的人:)

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

相关问题 如何从mongo数据库中完全删除文档的子文档 - How to delete a sub-document of a document completely from the mongo database 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website Angular Firestore - 显示子文档数组的内容 - Angular Firestore - Display contents of sub-document array 如何使用Mongoose查找子文档? - How to find a sub-document using Mongoose? 从Firestore文档获取字段 - Get field from Firestore document MongoDB:Map Reduce:从另一个创建一个子文档 - MongoDB : Map Reduce : Create one sub-document from another one 如何将数据保存到mongodb中的子文档?我正在将农场子文档转换为null - How to save data into sub-document in mongodb?Mine is converting farm sub-doc into null mongodb javascript:返回仅包含匹配子文档的文档 - mongodb javascript: return document with only matched sub-document 如何使用 typescript 或 javascript 从 firestore 中的文档获取字段值并放入 console.log()? - How can I get a field value from a document in firestore with typescript or javascript and put in a console.log()? Mongodb 检查数组的子文档中是否存在字段 - Mongodb check If field exists in an sub-document of an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM