ref.once() not called on Firebase

12,671

(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.

Share:
12,671
Michel
Author by

Michel

I am currently developing iOS software, mainly using Swift. With a total of about 20 apps on the appStore, the main ones being related to language learning. For some examples see: http://www.sofisfun.net/Language/ and http://www.sofisfun.net/Calculators/ I recently got to work on Heroku to port some projects from the Parse.com site to open source Parse-Server, that got me on the way to get my hands on Node.JS where I built this little prime number site to get familiar with the environment. And I more recently got interested in Clojure, where I built this as a trial project.

Updated on July 03, 2022

Comments

  • Michel
    Michel almost 2 years

    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.