i'm creating each document as:
const postCollection = collection(database, "posts");
const submitPost = async () => {
await addDoc(postCollection, {
title,
body,
comments: [{ userId: "", content: "", name: "" }],
likes: { count: 0, users: [] },
date: new Date().toLocaleDateString(),
user: { name: auth.currentUser.displayName, id: auth.currentUser.uid },
});
};
And then i'm retrieving each document as:
const postCollection = collection(database, "posts");
const getPost = async () => {
const data = await getDocs(postCollection);
const newData = data.docs.map((doc) => ({
...doc.data(),
id: doc.id,
}));
setPostList(newData);
};
But how can i sort them by date? From last to first. Thanks in advance
query()withwhere()functions. And you need to create a field in each document with Firebase Timestamp.