After experimenting with query and orderBy() methods, I'm still struggling to properly integrate it into my code. Here's what I have so far:
Methods:
async saveMessage(){
try {
const docRef = await addDoc(collection(db, "chat"), {
message:this.message,
createdAt: new Date()
});
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
this.message= null;
},
async fetchMessages(){
const querySnapshot = await getDocs(collection(db,"chat"));
let allMessages = [];
querySnapshot.forEach((doc) =>{
allMessages.push(doc.data());
})
this.messages = allMessages;
}
},