繁体   English   中英

无法检查云 Firestore 数据库中是否存在文档以及 reactjs

[英]Can't check if document exists in cloud firestore database and with 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);
                    }
                });
            });

代码仅在条件为真但不为假时执行。 我什至尝试输出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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM