我正在尝试创建一个聊天屏幕,以显示用户正在/的所有聊天((类似Facebook Messenger的东西,其中包含时间戳,lastMessage,conversationTitle和profilePicture)最后发送消息的人的图片)
这是我的JSON(我想将聊天频道信息与用户元信息结合起来)
"Chats" : {
"MAIN" : {
"isPrivateChannel" : false,
"lastMessageSent" : "Ye",
"messageUID" : "pdXI7oALkBRPK7Gyk4cjaiGspfU2",
"timestamp" : 1551844495595,
"title" : "Ye",
"users" : {
"ZAX7EnwfLRZU8IYRMLiVMRJm3mH2" : true,
"pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true
}
},
"ZAX7EnwfLRZU8IYRMLiVMRJm3mH2_pdXI7oALkBRPK7Gyk4cjaiGspfU2" : {
"isPrivateChannel" : true,
"lastMessageSent" : "Yo",
"messageUID" : "ZAX7EnwfLRZU8IYRMLiVMRJm3mH2",
"timestamp" : 1551844710716,
"title" : "Yo",
"users" : {
"ZAX7EnwfLRZU8IYRMLiVMRJm3mH2" : true,
"pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true
}
},
"desWJdrhJffGzp9Ou4tPFGyuGfO2_pdXI7oALkBRPK7Gyk4cjaiGspfU2" : {
"isPrivateChannel" : true,
"lastMessageSent" : "Ya",
"messageUID" : "desWJdrhJffGzp9Ou4tPFGyuGfO2",
"timestamp" : 1551844671583,
"title" : "Ya",
"users" : {
"desWJdrhJffGzp9Ou4tPFGyuGfO2" : true,
"pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true
}
}
},
"users" : {
"ZAX7EnwfLRZU8IYRMLiVMRJm3mH2" : {
"chats" : {
"MAIN" : true,
"ZAX7EnwfLRZU8IYRMLiVMRJm3mH2_pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true
},
"meta" : {
"about" : "Nice",
"email" : "teest3@test.com",
"profilePicture" : "https://platform-lookaside.fbsbx.com/platform/profilepic/?...PROFILE_PIC_WOULD_BE_HERE",
"username" : "test3"
}
},
"desWJdrhJffGzp9Ou4tPFGyuGfO2" : {
"chats" : {
"desWJdrhJffGzp9Ou4tPFGyuGfO2_pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true
},
"meta" : {
"about" : "Tall",
"email" : "test2@test.com",
"profilePicture" : "none",
"username" : "test2"
}
},
"pdXI7oALkBRPK7Gyk4cjaiGspfU2" : {
"chats" : {
"MAIN" : true,
"ZAX7EnwfLRZU8IYRMLiVMRJm3mH2_pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true,
"desWJdrhJffGzp9Ou4tPFGyuGfO2_pdXI7oALkBRPK7Gyk4cjaiGspfU2" : true
},
"meta" : {
"about" : "Smart",
"email" : "test@test.com",
"profilePicture" : "none",
"username" : "test"
}
}
}
我目前正在尝试使用快照内部的快照来执行此操作。我当时想,我只是下拉了相关的用户信息,而不必下载所有用户并在前端使用某种过滤器或地图。
这是我的代码:
export const getUserChannels = () => {
return dispatch => {
dispatch(loadChannels());
let currentUserID = firebaseService.auth().currentUser.uid;
firebaseService.database().ref("Chats").orderByChild('timestamp').once('value').then(snapshot => {
let data = []
snapshot.forEach((child) =>{
const childVal = child.val();
const promise = firebaseService
.database()
.ref("users/" + childVal.messageUID + '/meta').once("value").then(userSnapshot => {
return userSnapshot.val();
}).catch(error=> console.log(error))
data.push({...child.val(), ...promise})
})
return Promise.all(data)
}).catch(error => {
dispatch(loadChannelsError(error.message));
}).then( data => {
console.log("DATAAAA", JSON.stringify(data));
const filteredData = data.filter(chat => chat.users[`${currentUserID}`] === true);
dispatch(loadChannelsSuccess(filteredData));
})
}
}
这是我遇到的问题:
我无法弄清楚如何在forEach语句中获得承诺来解决。我还尝试了多种方法来解决promise和async await语法,但是在推送到数据之前无法获得解决的诺言。