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

PHP and MySQL WP -

javascript - Generate barcode from text and convert it to base64 -

osx - "SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure" when retrieving page through wget -