I have two collections containing messages from two users. The output of these to the DOM is fine, it displays the messages from user 1 first then user 2 or the other way. This is not good for the chat experience. I need to cycle through the documents first and order them chronologically.
function fetchMessage(){
dbRef= db.collection("user").doc("eF1RFw3FVAO3LFJ8ORrf7oJPyR13").collection('user1').orderBy("dbTime", "asc");
dbRef.get().then((snapshot) =>{
snapshot.docs.forEach(doc =>{
rendermeMessage(doc); //calls these method that outputs and
//renders to the DOM
});
});
}
This other one fetches messages from the second user collection in real-time
dbRef= db.collection("user").doc("eF1RFw3FVAO3LFJ8ORrf7oJPyR13").collection('user2').orderBy("sentAt", "asc");
dbRef.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if(change.type==="added"){
renderAdminMessages(change.doc);
}
});
});
I just need it to behave like a normal chat app,i.e the earliest message first, then the second message from the second user if it is the one that comes second in time, like that. I had created an array to first store all the documents from both users there but I cannot cycle through the array. Each document from each collection has a timestamp and a message by the way.