如何使用return语句返回值

时间:2018-10-15 19:33:25

标签: javascript firebase firebase-realtime-database

在检查用户的登录状态时,我想返回该人的用户名。 通过查找id密钥来检索userName。

问题在于以下代码返回错误 TypeError:未定义childData 堆栈跟踪(不知道那是什么意思)

enter image description here

function checkStatus(){
        firebase.auth().onAuthStateChanged(function(user){
            if (user) {
               var uid = user.uid;
               var a = functionReturnUserNameByID(uid);
               console.log(a)
          } else {
                // blabla
          }
        });
    }

function functionReturnUserNameByID(a){
    var key;
    var childData;
    firebase.database().ref('/dataman-831be/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {

        snapshot.forEach(function(childSnapshot) {
                key = childSnapshot.key;
                childData = childSnapshot.val();
                console.log(childData.userName);                    
        });
    });
    return childData.userName;
};

0 个答案:

没有答案