i have tried using query and orderBy() and firstly cant even properly fit it into my code
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;
}
},
To order results, you add a query with an ordering clause:
import { query, orderBy, ... } from "firebase/firestore";
const querySnapshot = await getDocs(query(collection(db,"chat"), orderBy("name")));
I recommend reading the Firebase documentation on ordering and limiting data, from where I got the changes I made to your code.