【问题标题】:Firestore Snapshot - Do we need to check if a returned doc .exists?Firestore 快照 - 我们是否需要检查返回的文档 .exists 是否存在?
【发布时间】:2021-11-23 20:13:51
【问题描述】:
想象一下这种情况:
const refs = [...];
const docs = await firestore.getAll(...refs);
从 getAll() 返回的文档是否可能不存在?
我的意思是,对我来说,获得一个结果不存在的文档是没有意义的......
有没有返回的doc不存在的特殊情况?
docs.forEach((doc) => console.log(doc.exists));
【问题讨论】:
标签:
node.js
google-cloud-platform
google-cloud-firestore
【解决方案1】:
是的。如果文档不存在,data() 将返回 undefined。虽然您可以过滤数组并删除任何不存在的文档,如下所示:
const refs = [...];
const docsSnaps = await firestore.getAll(...refs);
const docs = docSnaps.filter(d => d.exists).map(d => d.data())
它是QuerySnapshot,其中所有返回的文档都存在,因为它们与查询匹配。