Raul
Raul

Reputation: 3091

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

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));

Upvotes: 1

Views: 71

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50920

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.

Upvotes: 2

Related Questions