【问题标题】:Can't check if document exists in cloud firestore database and with reactjs无法检查云Firestore数据库中是否存在文档并使用reactjs
【发布时间】:2020-12-14 20:41:33
【问题描述】:

当我在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 值,但它只在其为真时输出

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore


    【解决方案1】:

    如果没有文档,则永远不会输入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

    【讨论】:

    • 非常感谢我一整天都在寻找。
    猜你喜欢
    • 2019-01-09
    • 2020-04-24
    • 2019-02-19
    • 2020-12-24
    • 2018-10-22
    • 2021-11-09
    • 2019-04-19
    • 2021-07-22
    • 2020-10-05
    相关资源
    最近更新 更多