在Javascript中检索Firebase子值

问题描述 投票:0回答:1
function sontinue() {
    var user = firebase.auth().currentUser;
    var uid = user.uid; 

    var adaRef = firebase.database().ref("User/" + uid);
    if (adaRef.orderByChild("Role").equalTo("admin")) {
        location.href = "DB.html";
    } else {
        location.href = "index.html";
    }

}

我想链接我的 "管理员" 账目 DB.html"用户" 账目 索引.html 但我想我总是在检索子值时失败。

This is my firebase database

javascript firebase firebase-realtime-database
1个回答
0
投票

你没有从服务器上检索数据。记住你需要调用.once('value')来获取你的查询,然后根据他们的值是admin还是user来迭代剩下的代码。Firebase Docs有更多的说明 我修改了你的代码,并把它放在下面

  function sontinue() {
            var user = firebase.auth().currentUser;
            var uid = user.uid; 

            var adaRef = firebase.database().ref("User/" + uid).orderByChild("Role");
            //you now need to retrieve the data

            return adaRef.once('value').then((snapshot)=>{
                return snapshot.forEach(snapshot=>{
                if (snapshot.child("Role").val()==="admin") {
                location.href = "DB.html";
                 } else {
                location.href = "index.html";
                }
                return console.log("added");
                })

            })


        }

如果你只是想知道谁是管理员类型的用户......我会用下面这个......效率更高。

   function sontinue() {
        var user = firebase.auth().currentUser;
        var uid = user.uid; 

        var adaRef = firebase.database().ref("User/" + uid).orderByChild("Role").equalTo("admin");
        //you now need to retrieve the data

        return adaRef.once('value').then((snapshot)=>{
            //you now have all the users that are just admins
            return snapshot.forEach(snapshot=>{
            location.href = "DB.html";
            return console.log("added");
            })

        })


    }
© www.soinside.com 2019 - 2024. All rights reserved.