typescript - variables outside of snapshot firebase Angular -


this may come across simple i've ran problem lot angular , need little explanation. i'm trying variable value outside of snapshot function. if this

let testvariable : string;  var testcount = firebase.database().ref('users/' + username + '/username');  testcount.on('value', function (snapshot) {  testvariable = snapshot.val();   });  alert(testvariable); //comes out undefined 

thank you

the problem code running asynchronously. means javascript make call firebase , call alert function, without waiting response. alert must either placed in callback or called after variable has been set.

if using "this" might have problems accessing within function. if so, change function syntax arrows this:

testcount.on('value', (snapshot) => {       this.testvariable = snapshot.val();   }); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -