【发布时间】:2018-10-26 09:23:38
【问题描述】:
我正在尝试返回一个值并将其显示在控制台中。(现在)
用户 ID 被传递给“函数”。
该函数将在数据库中检查此 ID,并应返回用户名。
我知道找到了正确的数据,因为 console.log 在函数本身中返回了正确的信息(请参阅:THIS WORKS)
但是使用return childData.userName;时返回值是“未定义”
调用函数
console.log( f_returnUserDetails(uid)
函数本身
function f_returnUserDetails(a){
console.log(a)
var key;
var childData;
firebase.database().ref('/dataman-blabla/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
snapshot.forEach(function(childSnapshot) {
key = childSnapshot.key;
childData = childSnapshot.val();
console.log(childData.userName); //THIS WORKS
return childData.userName; //THIS DOES NOT
});
});
};
【问题讨论】:
-
您还需要在 firebase 对象之前添加 return 并检查
-
firebase 对象之前是什么意思,那在哪里?
标签: javascript firebase