Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在下面的代码段中无法访问中的变量值?_Javascript_Variables_Scope_Alert - Fatal编程技术网

Javascript 为什么在下面的代码段中无法访问中的变量值?

Javascript 为什么在下面的代码段中无法访问中的变量值?,javascript,variables,scope,alert,Javascript,Variables,Scope,Alert,我有以下Javascript代码片段: authData=ref.getAuth(); if(authData == null){ //TODO find an elegant way to manage authorization // window.location = "../index.html"; } else { ref.child("users").child(authData.uid).on("value", function(snapshot)

我有以下Javascript代码片段:

authData=ref.getAuth();

if(authData == null){
    //TODO find an elegant way to manage authorization 
    //  window.location = "../index.html";
} else {
      ref.child("users").child(authData.uid).on("value", function(snapshot){
      $( "span.user-name").html(snapshot.val().displayName);    
      loggedInUser.displayName = snapshot.val().displayName;
      //alert("Name inside : "+loggedInUser.displayName);
      //Here it displays the value      
    });         
}

alert("Nameada is out : "+loggedInUser.displayName);
//Here it shows 'undefined' 
为什么?

我想使用变量值
loggedInUser.displayName
我在哪里显示了警报

有人能帮我访问值并显示警报吗


谢谢。

当回调函数(
函数(快照){…}
)尚未调用时,将执行最后的
警报。请注意,回调函数是异步调用的,因此只有在当前运行的代码完成并触发
value
事件后,才会执行回调函数

这也解释了内部(注释掉的)
警报
工作的原因。请注意,这段代码(回调函数)的执行时间晚于另一段
警报
,即使它在代码中出现得更早

您可以通过在回调中调用另一个函数来“解决”它,如下所示:

authData=ref.getAuth();

if(authData == null){
    //TODO find an elegant way to manage authorization 
    //  window.location = "../index.html";
} else {
    ref.child("users").child(authData.uid).on("value", function(snapshot){
        $( "span.user-name").html(snapshot.val().displayName);    
        loggedInUser.displayName = snapshot.val().displayName;
        whenUserLogged();
    });         
}

function whenUserLogged() {
   alert("Name : "+loggedInUser.displayName);
   // anything else you want to do....
}
一些改进建议 不要使用(在代码中,所有变量都是全局变量),而是将变量作为函数参数传递


您可能需要查看。

当回调函数(
函数(快照){…}
)尚未调用时,将执行最后的
警报。请注意,回调函数是异步调用的,因此只有在当前运行的代码完成并触发
value
事件后,才会执行回调函数

这也解释了内部(注释掉的)
警报
工作的原因。请注意,这段代码(回调函数)的执行时间晚于另一段
警报
,即使它在代码中出现得更早

您可以通过在回调中调用另一个函数来“解决”它,如下所示:

authData=ref.getAuth();

if(authData == null){
    //TODO find an elegant way to manage authorization 
    //  window.location = "../index.html";
} else {
    ref.child("users").child(authData.uid).on("value", function(snapshot){
        $( "span.user-name").html(snapshot.val().displayName);    
        loggedInUser.displayName = snapshot.val().displayName;
        whenUserLogged();
    });         
}

function whenUserLogged() {
   alert("Name : "+loggedInUser.displayName);
   // anything else you want to do....
}
一些改进建议 不要使用(在代码中,所有变量都是全局变量),而是将变量作为函数参数传递


您可能需要查看。

您确定authData真的不是空的吗?getAuth()调用可能返回null,然后该变量将永远不会初始化。我没有看到
loggedInUser
变量的声明。它在哪里?您的变量可能超出了正确的范围。@YasserElsayed,ray:所有这些都存在,并且已经在其他代码文件中初始化,这些文件也包含在内。请不要担心,认为它们是工作的。你确定AuthDATA真的不是空的吗?getAuth()调用可能返回null,然后该变量将永远不会初始化。我没有看到
loggedInUser
变量的声明。它在哪里?您的变量可能超出了正确的范围。@YasserElsayed,ray:所有这些都存在,并且已经在其他代码文件中初始化,这些文件也包含在内。请不要担心,并考虑它们正在工作。告诉我该如何访问变量?您可以访问回调函数内的变量。或者,您可以在该位置调用函数,并在新函数中输出变量。告诉我如何访问变量?您可以访问回调函数中的变量。或者,您可以在该位置调用函数,并在该新函数中输出变量。