Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get Id of documents in Firestore using useCollectionData hook

const messagesRef = collection(db, 'messages')
const q = query(messagesRef, orderBy('createdAt'), limit(25))
const [messages, loading] = useCollectionData(q)
// messages print fields of documents without id

Need to get Id of documents, I guess there's a way to pass options in that hook but dk how.

If there's no way to get Id using that hook then what's the option?

like image 908
codelover1 Avatar asked Dec 06 '25 07:12

codelover1


1 Answers

Well, I changed hook to useCollection

const [messages, loading] = useCollection(q)

This solved a problem. useCollectionData only returns fields of documents, while useCollection returns snapshot with every document and id.

like image 169
codelover1 Avatar answered Dec 08 '25 19:12

codelover1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!