【发布时间】:2021-11-01 03:13:20
【问题描述】:
我想创建一个实用函数,根据集合中是否存在 Firestore 文档返回 true 或 false。
下面是我最初写的,但它返回一个承诺而不是布尔值。
async function docExists(docName, docId) {
const docRef = db.collection(docName).doc(docId);
await docRef.get().then((docSnapshot) => {
if (docSnapshot.exists) {
return true
} else {
return false
}
});
}
有没有办法让它返回一个布尔值,或者这只是解决问题的错误方法?
【问题讨论】:
-
您返回的是您传递给
.then()的回调函数,而不是docExists函数。docExits函数不返回任何内容,但由于它是async,因此它返回一个空承诺。用return docSnapshot.exists;替换你的 if 块(你不需要)并在await之前放一个return。
标签: javascript firebase google-cloud-firestore promise