1

I have a firebase reference, where I pull data down for a specific custom index I created.

requestsRef
    .orderByChild('systemgameindex')
    .startAt(lastrequest.systemgameindex.toString())
    .endAt(lastrequest.systemgameindex.toString() + '~')
    .limitToFirst(customElem.dataops.limit + 1)
    .on('child_added', function (snapshot) {
        var request = snapshot.val() || {};

        request.key = snapshot.key();
        request.systemColor = customElem.getSystemColor(request.system);
        request.description = customElem.truncateText(request.description, 65);
        customElem.getUserProfile(request);
        customElem.getCommentCount(request.key);

        if (request.systemgameindex !== lastrequest.systemgameindex) { customElem.push('requests', request); };
            customElem.removeSpinnerRoo();
        });

Right before I make the call to firebase, I have a custom spinner I dislay with a function called addSpinnerRoo(), and when data is returned, I make a call to removeSpinnerRoo() to hide the spinner on the DOM.

It works beautifully when there's data to return from firebase, but if the firebase query brings back no results, the callback on child_added never gets fired, so I have a spinner still spinning on the DOM.

Is there a way to handle when there's no data returned within Firebase?

Any insight would be appreciated a lot. Thanks

Manju
  • 668
  • 7
  • 22
Michael Onubogu
  • 63
  • 1
  • 1
  • 10
  • possible duplicate of [how to discard initial data in a Firebase DB](http://stackoverflow.com/questions/19883736/how-to-discard-initial-data-in-a-firebase-db) – Kato Aug 04 '15 at 20:03
  • possible duplicate of [how to retrieve only new data](http://stackoverflow.com/questions/18270995/how-to-retreive-only-new-data) – Kato Aug 04 '15 at 20:04

1 Answers1

2

After reading this from the documentation from here:

The callback function receives a DataSnapshot, which is a snapshot of the data. A snapshot is a picture of the data at a particular database reference at a single point in time. Calling val() on a snapshot returns the JavaScript object representation of the data. If no data exists at the reference's location, the snapshots value will be null.

I was able to do use "val" instead of "child_added" to actually have firebase still fire the callback for the ".on()" method. So my code now looks like this:

var data = snapshot.val();

if (data !== null && data !== undefined) {
    var requests = _.map(data, function (val, key) {
        val.key = key;
        return val;
    });

    _.each(requests, function (request) {

        request.systemColor = customElem.getSystemColor(request.system);
        request.description = customElem.truncateText(request.description, 65);
        customElem.getUserProfile(request);
        customElem.getCommentCount(request.key);
        customElem.push('requests', request);
    });
}
customElem.removeSpinnerRoo();

And with that, I was able to get what I needed. If this helps anyone, great...

Michael Onubogu
  • 63
  • 1
  • 1
  • 10
  • Keep in mind that `value` will be fired any time any value of any child node is changed and will download the entire collection. So probably best not to use this with on() for collections. – Kato Aug 04 '15 at 20:03
  • Yeah you're right. I realized this after I had made the change. It seems child_added is still what I need, I just dont know why it does not fire the callback regardless of whether or not any data is returned like "value" does – Michael Onubogu Aug 05 '15 at 15:02
  • 1
    See the links I shared. That's the best way to determine if "initial data" has loaded, although, generally speaking, this is a flawed representation of Firebase and means you're not really embracing the real-time aspects. I'd recommend skipping the spinner--Firebase data usually takes milliseconds to return anyway--and the ideas of initial vs future data. – Kato Aug 05 '15 at 19:58