2014-11-15 3 views
0

user_properties이 내 함수 외부에 있다고 선언합니다. 그런 다음 firebase에서 데이터를 가져와 user_properties에 저장하려고 시도하지만 정의되지 않은 상태가됩니다.변수가 함수 내에서 업데이트 될 때 정의되지 않음

내가 잘못하고있는 것을 아는 사람이 있습니까?

var user_properties; 

get_user_properties = new Firebase("https://<MY-URL>/users/"+auth.uid+"/properties"); 
get_user_properties.once('value', function (dataSnapshot) { 
    user_properties = dataSnapshot.val(); 
}); 

//undefined 
console.log(user_properties); 
+0

기능은 비동기로

var foo, obj = new Bar("some text"); obj.set("cute cat", function(cat){ foo = "my lovely cat"; }); console.log(foo); // undefined obj.doo(); console.log(foo); // "my lovely cat" 

답변

0

간단한 예제를 제공했습니다. 콘솔 로그 요청 었소 아직 완료라고이 생성자 함수

function Bar(c){ 
    this.mcat = c; 
    this.callback; 

    /* this method only saves that callback function */ 
    this.set = function(catname, callback){ 
     this.mcat = catname; 
     this.callback = callback; 
    } 

    /* this method calls that callback function */ 
    this.doo = function(){ 
     this.callback(); 
    } 
} 
관련 문제