[英]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
已经是子文档中的非地图字段,可以使用上述代码轻松从数据库中拉出。
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.