Ionic & Firebase + Best Practice accessing data which might not exist?


#1

I’ve chosen to use Firebase as a simple means to handle authentication and store my over-the-air data… but I’m running into an issue (occasionally).

I’m using angularFire, so whenever I access data I have the ability to get a $firebaseObject, $firebaseArray or null.

The null part is what catches me off guard… None of the tutorials or demos I see go through how to handle the null aspect of the request… they just go ahead and bind the data to $scope or check if it’s $loaded() all things which wont work with null; and then this cascades into other issues surfacing and potentially just white/empty views.

So how, or what, is the best way to access data from Firebase that might not be there?


#2

I’m not sure exactly what your question is, but I think it’s regarding dealing with async data. The problem is it’s null because the data hasn’t been returned yet. Do a little experiment, set a timeout for 3-5 secs and check if the value is there using console.log. It should be at that point.

To fix it, you’ve got to use promises. Check out http://stackoverflow.com/questions/30273858/handle-asynchronous-behavior-firebase-in-angularjs. Also remember if you’re using a series of queries you’ve got to chain your promises otherwise a val you need won’t be there. The angular docs on $q kinda suck, but here’s a link https://docs.angularjs.org/api/ng/service/$q.