This question already has an answer here:
这个问题已经有了答案:
- How do I return the response from an asynchronous call? 32 answers
- 如何从异步调用返回响应?32个答案
This is my code:
这是我的代码:
document.getElementById('revealUser').onclick = displayDaUsers
function displayDaUsers(){
pullAllUsersFromDB();
debugger;
}
function pullAllUsersFromDB(){
rootRef.child('users').on('value', function(snapshot) {
var users_array = [];
var users_object = snapshot.val();
Object.keys(users_object).map(function(key) {
users_array.push(users_object[key]);
});
// window.dateApp.allUsers = users_array;
return users_array
});
}
html:
html:
<input type="submit" id="revealUser" value="reveal user">
I put a debugger in to see the problem but it does not help. When I go into the console and type in users_array
I get Uncaught ReferenceError: users_array is not defined(…)
我放了一个调试器来查看问题,但它没有帮助。当我进入控制台并输入users_array时,我将得到未捕获的ReferenceError: users_array没有定义(…)
NEW CODE (EDIT):
新代码(编辑):
according to another * answers this should work..
根据另一个*的回答,这应该起作用。
function displayDaUsers(){
var test = pullAllUsersFromDB();
console.log(test);
//pullAllUsersFromDB();
//debugger;
//setUpFirstUser()
}
function pullAllUsersFromDB(){
rootRef.child('users').on('value', function(snapshot) {
var users_array = [];
var users_object = snapshot.val();
Object.keys(users_object).map(function(key) {
users_array.push(users_object[key]);
});
//window.dateApp.allUsers = users_array;
return users_array
});
}
1 个解决方案
#1
0
The return value users_array
is local to the scope of the anonymous callback function function(snapshot) {...
. In other words, its value is lost outside of that scope.
返回值users_array是本地的范围匿名函数回调函数(快照){ ....换句话说,它的价值不在这个范围之内。
At what point in your logic do you need access to user_array
? If you need access to it outside of the context of your functions, maybe it makes sense to define a variable with greater scope, and then setting its value in your anonymous function. E.g.
在您的逻辑中什么时候需要访问user_array?如果您需要在函数的上下文之外访问它,那么定义一个具有更大范围的变量,然后在您的匿名函数中设置它的值是有意义的。如。
document.getElementById...
var arr;
...
function pullAllUsersFromDB() {
...
...function(snapshot) {
arr = users_array;
});
}
// pullAllUsersFromDB() (and its callback) must be called for arr to be defined at this point in execution
#1
0
The return value users_array
is local to the scope of the anonymous callback function function(snapshot) {...
. In other words, its value is lost outside of that scope.
返回值users_array是本地的范围匿名函数回调函数(快照){ ....换句话说,它的价值不在这个范围之内。
At what point in your logic do you need access to user_array
? If you need access to it outside of the context of your functions, maybe it makes sense to define a variable with greater scope, and then setting its value in your anonymous function. E.g.
在您的逻辑中什么时候需要访问user_array?如果您需要在函数的上下文之外访问它,那么定义一个具有更大范围的变量,然后在您的匿名函数中设置它的值是有意义的。如。
document.getElementById...
var arr;
...
function pullAllUsersFromDB() {
...
...function(snapshot) {
arr = users_array;
});
}
// pullAllUsersFromDB() (and its callback) must be called for arr to be defined at this point in execution