codelover1
codelover1

Reputation: 171

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?

Upvotes: 3

Views: 953

Answers (1)

codelover1
codelover1

Reputation: 171

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.

Upvotes: 2

Related Questions