here i trying to get the collection("messages") snapshot which conatains document, but i want to get the collections snapshot which shoud be order by "timestamp" feild of documents in decending order how could i do that useing js v9 of firebase
onSnapshot(collection(db,"channels",channelId,"messages"),(snapshot) => {
setMessages(snapshot.docs.map(doc => doc.data()))
})
You can use query() function to build a query and then orderBy() function to specify the Query Constraint. Try the following:
import { collection, query, onSnapshot, orderBy } from "firebase/firestore"
const colRef = collection(db, "channels", channelId, "messages"), orderBy('timemstamp', 'desc'))
const q = query(colRef, orderBy("timestamp", "desc"))
onSnapshot(q, (snapshot) => {
setMessages(snapshot.docs.map(doc => doc.data()))
})