gpt4 book ai didi

javascript - 当 firebase 数据存储到对象时获取未定义

转载 作者:行者123 更新时间:2023-12-01 02:43:46 25 4
gpt4 key购买 nike

这是我的代码。我想将 Firebase 中的数据存储到一个对象 (this.chatThread)。

chatThreadMessageFetch(mobileNumber, fromID) {

this.chatThreads=[];
return firebase.database().ref(messaging/${mobileNumber}/chatThread/${fromID}).orderByKey()
.once('value').then(snapshot => {

snapshot.forEach((messageSnapshot)=> {

key=messageSnapshot.key*-1;

this.chatThreads[key]=messageSnapshot.val();

});
});

}

当我通过控制台记录 this.chatThreads 时,我得到了undefined。以下是我的 firebase 数据库。 enter image description here

相反,如果我分配 this.chatThreads = messageSnapshot.val()

,我就会获取数据

最佳答案

您可以使用push来存储数据

this.chatThreads[key].push(messageSnapshot.val());

this.chatThreads.push(key+':'+messageSnapshot.val());

关于javascript - 当 firebase 数据存储到对象时获取未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47390934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com