我创建了一个有趣的小消息应用程序,并且尝试在用户之间进行对话,并且进行了联接查询,因为我在Firebase上对规范进行了规范化。
for(i=0;i<5;i++)
{
cout<<"Student num"<<" "<<i<<endl;
cout<<"Enter P , C , M Marks"<<endl;
cin>>ob[i].p;
cin>>ob[i].c;
cin>>ob[i].m;
}
{
"messages": {
"-LZtqlYn2WB_1E-u4gUk-LZtqlYn2WB_1E-u4gUo": {
"-LZv3-yzpZ88lCLkGyRT": {
"createdAt": 1551474102199,
"receiverId": "-LZtqlYn2WB_1E-u4gUo",
"senderId": "-LZtqlYn2WB_1E-u4gUk",
"text": "alls"
}
},
"-LZtqlYn2WB_1E-u4gUo-L_8ymxVU_bS8r9Rux4y": {
"-L_8z5l0mNgJodbdh07O": {
"createdAt": 1551724473404,
"receiverId": "-LZtqlYn2WB_1E-u4gUo",
"senderId": "-L_8ymxVU_bS8r9Rux4y",
"text": "asfasfsf"
}
}
},
"users": {
"-LZtqlYn2WB_1E-u4gUo": {
"conversations": {
"-L_8ymxVU_bS8r9Rux4y": {
"conversationId": "-LZtqlYn2WB_1E-u4gUo-L_8ymxVU_bS8r9Rux4y",
"unseenCount": 5
}
},
"createdAt": 1551453853939,
"image": "https://d35arkf8909z2a.cloudfront.net/new_user.png",
"name": "John"
},
"-L_8ymxVU_bS8r9Rux4y": {
"createdAt": 1551724392288,
"image": "https://d35arkf8909z2a.cloudfront.net/new_user.png",
"name": "Meryl"
}
}
}
答案 0 :(得分:0)
我已经知道了,但是我不知道这是否是最好的方法。我认为我的问题是关于我已经像下面的代码那样解决了promises数组:
export function fetchConversations(userId) {
return (dispatch) => {
dispatch(fetchingConversations());
const users = [];
const messages = [];
const rootRef = firebase.database().ref();
return rootRef
.child('users')
.child(userId)
.child('conversations')
.once('value')
.then((conversationSnapshots) => {
const promises = [];
conversationSnapshots.forEach((conversation) => {
const { conversationId } = conversation.val();
promises.push(
rootRef
.child('users')
.child(conversation.key)
.once('value')
.then((user) => {
const _user = Object.assign({ id: conversation.key }, user.val());
users.push(_user);
}),
);
promises.push(
rootRef
.child('messages')
.child(conversationId)
.limitToLast(1)
.once('value')
.then((message) => {
const _message = message.val();
messages.push(_message[Object.keys(_message)[0]]);
}),
);
});
return Promise.all(promises);
})
.then(() => {
const data = [];
for (let i = 0; i < users.length; i++) {
data.push({
id: users[i].id,
receiver: { name: users[i].name, image: users[i].image },
lastMessage: messages[i].text,
lastMessageDate: moment(messages[i].createdAt),
});
}
dispatch(fetchingConversationsSuccess(data));
});
};
}