Home > other >  Firestore Snapshot - Do we need to check if a returned doc .exists?
Firestore Snapshot - Do we need to check if a returned doc .exists?

Time:10-03

Imagine this situation:

const refs = [...];
const docs = await firestore.getAll(...refs);

Is it possible that a returned doc from the getAll() doesn't exist?

I mean, for me it hasn't got sense to get a doc that doesn't exist as result...

Is there any special situation where a returned doc does not exist?

docs.forEach((doc) => console.log(doc.exists));

CodePudding user response:

Yes. The data() will return undefined if the document does not exist. Though you can filter the array and remove any non-existing documents as shown below:

const refs = [...];
const docsSnaps = await firestore.getAll(...refs);

const docs = docSnaps.filter(d => d.exists).map(d => d.data())

It's the QuerySnapshot in which all documents returned exists since they match the query.

  • Related