【发布时间】:2017-11-26 07:55:34
【问题描述】:
我正在使用 firebase 和 javascript。从两个表执行查询后,我试图从 firebase-database 返回一个数组。当我 console.log 我的数据时,我会为每一位数据获得一个单独的数组和对象。
var userId = 'hirerId';
var chatIdRef = firebase.database().ref("members");
var chatsRef = firebase.database().ref("chats");
chatIdRef.child(userId).on('child_added', snap => {
chatsRef.child(snap.key).once('value', snap => {
items = [];
items.push({
text: snap.val().text,
chatId: snap.key
});
console.log(items);
});
});
这会记录两个单独的数组和对象:[{"text":"How are you","chatId":"chatId"}] [{"text":"Hi friend","chatId":"chatId2"}]
我想要的结果是[{"text": "How are you","chatId":"chatId"}, {"text":"Hi friend","chatId":"chatId2"}]
这是我的数据结构: data structure
我怎样才能达到我想要的结果?谢谢
【问题讨论】:
-
看看
Array.concat()
标签: javascript arrays firebase react-native firebase-realtime-database