0

I've looked everywhere, and no place has given me a satisfactory answer; how do I just simply retrieve data from the Firebase Database when I want it? I don't want to syncronize the data, I just want to get the data when I call for it. Various attempts using once() have failed entirely, not getting me close at all. Is it even possible or is Firebase just not designed for what I'm trying to do?

EDIT: Minimal example of what I want to do:

let ref = firebase.database().ref("location1");
let intendedValue = "Data not Gathered";
ref.once("value").then( function(snapshot) {
  intendedValue = snapshot.val();
});
console.log(intendedValue);

Where my firebase database has the structure:

projectFolder:
    location1:
        datapoint1: "dataValue1";

So you'd think this would log "dataValue1", but instead it logs "Data not Gathered" as if ref.once() never even ran.

Vedvart1
  • 302
  • 4
  • 21

2 Answers2

1

since the call to

ref.once("value").then( function(snapshot) { intendedValue = snapshot.val(); });

is basically a callback, so the moment you log the output, the line intendedValue = snapshot.val(); is not even run yet.

You can try this

 ref.once("value").then( function(snapshot) {
  intendedValue = snapshot.val();
  console.log(intendedValue);
});
Kim G Pham
  • 145
  • 6
-1
function fire_base_db(){ 

           var ref = firebase.database().ref();
       ref.on("value", function(snapshot) {
console.log(snapshot.val());

}, function (error) {
console.log("Error: " + error.code);
     });}

snapshot.val() contains the value of your databse you can store the value of snapshot.val() in a variable and send it to another function

Akash
  • 195
  • 1
  • 3
  • 14