简体   繁体   English

Firestore WEB 触发事件

[英]Firestore WEB trigger event

I'm running my db to verify if "id" is already in use, if not, i can do my register, else, i cant user this id and i have to try another.我正在运行我的数据库来验证“id”是否已被使用,如果没有,我可以进行我的注册,否则,我不能使用这个 id,我必须尝试另一个。

code:代码:

db.collection("places").get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
    // console.log(doc.id)
    db.collection("places").doc(doc.id).collection("Local").where("id", "==", id_user)
        .get()
        .then(function (querySnapshot) {
            querySnapshot.forEach(function (doc2) {
                //cpnj is already registred
                if (doc2.exists) {
                    console.log("id already exist")
                    document.getElementById("preloader").style.display = "none";
                    alert("id in use, try another id");
                } else{
                    //register the user
                }
            });
        })
        .catch(function (error) {
            console.log("Error getting documents: ", error);
        });
    });
});

The problem is when id doesn't exist, the "else" inside the function is not "actived" because the Firebase condition".where("id", "==", id_user)" doesn't exist, so the query is not done.问题是当 id 不存在时,函数内的“else”不是“actived”,因为 Firebase 条件“.where("id", "==", id_user)" 不存在,所以查询没有完成。

How can i trigger this and after check that the "id" is not in use and proceed with registration?在检查“id”未在使用中并继续注册后,如何触发此操作?

Within a loop over a query snapshot, the document will always exist.在查询快照的循环内,文档将始终存在。 If there are no results, the query snapshot will be empty and it'll right now simply not enter your forEach loop.如果没有结果,则查询快照将为空,并且现在不会进入您的forEach循环。

What you're looking for is whether the query snapshot has any results, which you can do with:您正在寻找的是查询快照是否有任何结果,您可以使用:

if (querySnapshot.empty) {
    //register the user
} else{
    console.log("id already exist")
    document.getElementById("preloader").style.display = "none";
    alert("id in use, try another id");
}

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

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