简体   繁体   中英

How do I check for a specific string to get from a Firebase Database to filter what data is received?

Explaining my code below, I'm trying to see if the userType is equals to admin and set it to either user or admin. Unfortunately my if condition is not working and I have no idea how to go into the else statement. Currently I'm logged into the dummy user email.

这是我的数据库结构的图像。

firebase.auth().onAuthStateChanged((user) => { 
    console.log(user.email);
    console.log(user.uid);
    var standardUserType = "user";
    //firebase().database().ref('adminUIDs').equalTo(user.uid);
    //firebase().database().ref().child('accounts').child(user.uid).orderByChild('userType').equalTo(standardUserType).on("value", function(snapshot) {
    firebase().database().ref('adminUIDs').equalTo(user.uid).on("value", function(snapshot) {
        if (snapshot.exists() && user) {
            console.log("[admin] user data has been pushed to database");
            firebase.database().ref().child('accounts').child(user.uid).set({
                email: user.email,
                userId: user.uid,
                userType: "admin"
            })
        }else{
            console.log("[user] user data has been pushed to database");
            firebase.database().ref().child('accounts').child(user.uid).set({
                email: user.email,
                userId: user.uid,
                userType: "user"
            })
        }
        });
    console.log(checkUserType);

});

You're trying to look up a node whose key you know. For that you don't need a query, but can use a direct lookup (which also has the advantage that it happens at a fixed speed, not depending on the number of child nodes under adminUIDs ).

The code for it is:

firebase().database().ref('adminUIDs').child(user.uid).on("value", function(snapshot) {
    firebase.database().ref().child('accounts').child(user.uid).set({
        email: user.email,
        userId: user.uid,
        userType: snapshot.exists() ? "admin" : "user"
    })
});

I also simplified your if condition into a ternary expression, to reduce the amount of duplicate code.

Unfortunately my if condition is not working and I have no idea how to go into the else statement

This means that (snapshot.exists() && user) is always evaluating to true, when in fact you don't want it to (at least once, I assume).

Whenever logging in, onAuthStateChanged will be triggered with a non-null user object. But it is the snapshot.exists() part which probably needs to be changed to more accurately reflect what you're trying to do.

The docs for snapshot.exists say "Returns true if this DataSnapshot contains any data. It is slightly more efficient than using snapshot.val() !== null".

Looking at the image from your database structure, it appears that even when isAdmin is false, that path, adminUIDs/{theUserID} will still have some non-null value , specifically, {isAdmin: false} .

So changing (snapshot.exists() && user) to (snapshot.exists() && user && snapshot.val().isAdmin) could do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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