![](/img/trans.png)
[英]How to delete a sub-document of a document completely from the mongo database
[英]Firestore/Javascript: How to get a data from map field of a sub-document?
我想從我在 firestore 中的子文檔的地圖中獲取特定數據並將其顯示在我的 Web 應用程序頁面上。 我真的不知道如何在var.textContent = doc.data().
期間調用數據var.textContent = doc.data().
部分...
注:stages 為現場圖
幫助解決我的問題的圖片:
到目前為止,這就是我所擁有的:
<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>
編輯
為了進一步澄清我的問題,是否可以使用doc.data()
在 firestore 中獲取字段映射的內容? 因為我一直在使用doc.data()
來獲取子文檔的字段。
例子
因為我一直在使用
doc.data()
來獲取子文檔的字段。
我設置非地圖字段的代碼:
minutesAsleep.textContent = doc.data().minutesAsleep;
請注意: minutesAsleep
已經是子文檔中的非地圖字段,可以使用上述代碼輕松從數據庫中拉出。
在試驗和修改代碼之后:
remSleep.textContent = doc.data();
lbremSleep.textContent = "Minutes of REMSleep: ";
替換為:
remSleep.textContent = doc.data().stages.remSleep;
lbremSleep.textContent = "Minutes of REMSleep: ";
進一步解釋:
doc.data().
獲取文檔數據
然后,
doc.data().stages
獲取地圖字段階段下的文檔數據
最后,
doc.data().stages.remSleep;
獲取map field stage下的docs數據,獲取該map下remSleep的數據。
我希望這可以幫助任何人,尤其是那些使用 javascript 和 firebase 進行開發的人:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.