Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ref.once() not called on Firebase

I am puzzeled with the following issue. While making a little web page to see some data I have on Firebase, there is some place where I cannot access what I want. Of course I know there is something at this place on the server. Here is the code, it seems localRef.once() is never called.

var dbLocalURL = "https://atinytestapp.firebaseio.com/MyList/rc99/";
var localRef = new Firebase(dbLocalURL);
localRef.once("value", function(snapshot) {
    // WE NEVER COME HERE !!!!
    for (record in snapshot.val()) {
        document.write(record)
        var recordRef = new Firebase(dbLocalURL+record+"/");
        recordRef.once("value", function(rcdSnapshot) {
            var rcdData = rcdSnapshot.val();
            document.write(rcdData[“someKey”])
        })
    }
}, function (errorObject) {
    // WE NEVER COME HERE !!!!
    console.log("The read failed: " + errorObject.code);
    document.write("The read failed: " + errorObject.code);
});

My page accessing the data is working as it should about everywhere, except in one place with the above behaviour. Beside I have also checked that the URL I use is correct, by pasting it into a browser and seeing the data I expect. What can I be missing? I hope someone can give me a hint.

like image 855
Michel Avatar asked May 29 '16 04:05

Michel


People also ask

How do you reference Firebase?

You can reference the root or child location in your Database by calling firebase. database(). ref() or firebase. database().

What is DataSnapshot in Firebase?

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.

What is database ref Firebase?

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. This class is the starting point for all Database operations.


1 Answers

(note: this is the new Firebase 3 way) This wasn't included in the code in the question but here's the initialization on (here for completeness)

// Set the configuration for your app: Replace with your project's config object
  var config = {
    apiKey: '<your-api-key>',
    authDomain: '<your-auth-domain>',
    databaseURL: '<your-database-url>',
    storageBucket: '<your-storage-bucket>'
  };
  firebase.initializeApp(config);

  // Get a reference to the database service
  var database = firebase.database();

and assuming you want to just read the data once:

firebase.database().ref('MyList/rc99').once('value').then(function(snapshot) {
    for (record in snapshot.val()) { //unordered records, see comment
       document.write(record)
       ...

Firebase 2

var ref = new Firebase("https://atinytestapp.firebaseio.com/MyList/rc99/");

ref.on("value", function(snapshot) {
  snapshot.forEach(function(record) { //ordered records, see comment
    document.write(record)
    ...
});

Note: make sure the url is correct as if that's malformed it won't work at all. I tried your url and it doesn't seem to be responding.

like image 91
Jay Avatar answered Oct 12 '22 05:10

Jay