Home > Software design >  How to get a Firestore id of stored item in React?
How to get a Firestore id of stored item in React?

Time:08-08

I'm storing some data on the Firestore, it's basically a collection of posts. So I need to get a Firestore collection id of the particular item. I could do that like this :

  const data = await getDocs(postsDataRef); //getting array of items

  const id = (data.docs[1]._key.path.segments[6]); // choosing particular data

But maybe there is a special method for such operation? Thanks!

CodePudding user response:

If you're getting all docs, you can print the IDs of all of them with:

data.forEach((doc) => {
  console.log(doc.id);
})
  • Related