I'm trying to retrieve data from database and store the value into a variable, but it gives me undefined value which means variable is not storing the data.
var n1;
var reff=firebase.database().ref('login-demi/');
reff.on("child_added", function(data){
var newnum=data.val();
n1=newnum.number; //4
if (n1==4){
n1=newnum.age;
}
});
console.log(n1);
But if I try to "put console.log(n1)" inside the function under "n1=newnum.age;" then it will display the value, but i want the variable to store the value and display it outside the function can I know how can I do that?