无法检查云火库数据库中是否存在文档并使用 reactjs



我在做一个简单的项目时,在firestore数据库代码中遇到了一个问题,当我检查文档是否存在时,它只会在应该进行时返回true,而不会返回false事件

我的代码:

db.collection("Users")
.where("email", "==", state.email_value).get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
if(doc.exists) {
console.log("Document Exist");
} else {
console.log("Document Doesn't Exist);
}
});
});

只有当条件为true而不是false时,代码才会执行。我甚至尝试输出doc.exists值,但它只在其真实时输出

如果没有文档,则永远不会输入querySnapshot.forEach(function(doc) ...

相反,您需要检查查询本身是否有结果:

db.collection("Users")
.where("email", "==", state.email_value).get()
.then(function(querySnapshot) {
if (!querySnapshot.empty) {
console.log("Document Exist");
}
else {
console.log("Document Doesn't Exist");
}
});

对于这种情况,我强烈建议将Firebase的参考文档放在手边:https://firebase.google.com/docs/reference/js/firebase.firestore.QuerySnapshot

最新更新