RajatP
RajatP

Reputation: 11

How to store Firebase Database values in JavaScript?

I've been trying to store local variable's value into Global variable, but that just doesn't seem to work. Any help on this?

var xv = 0;
var yv = 0;

var itemx = firebase.database().ref().child("item").child("xval");
itemx.on('value', function(xvalue){
    xv = xvalue.val(); 
});

var itemy = firebase.database().ref().child("item").child("yval");
itemy.on('value', function(yvalue){
    yv = yvalue.val();
});

var start = graph.grid[aa][bb];
var end = graph.grid[xv][yv];
var result = astar.search(graph, start, end);

I'm unable to fetch the values of variables xv and yv outside the Firebase function.

Upvotes: 1

Views: 433

Answers (1)

Himanshu
Himanshu

Reputation: 12675

you cannot save an object to a variable defined as integer. Declare the variables without initializing them with any value like this and you are good to go.

var xv, yv;

var itemx = firebase.database().ref().child("item").child("xval");
itemx.on('value', function(xvalue){
    xv = xvalue.val(); // it contains an object to view it print on console.
    console.log(xvalue.val())
});

var itemy = firebase.database().ref().child("item").child("yval");
itemy.on('value', function(yvalue){
    yv = yvalue.val();
});

Upvotes: 1

Related Questions