Proper use of hasChild () and forEach () from a link in Firebase?

With my limited understanding and SQL base, I don't quite understand how to use hasChild () and forEach (); they feel that they belong to a link, not a snapshot.

(I will use hasChild () for the rest of the discussion, but the concepts are interchangeable.)

Here are my thoughts:

  • I have a Firebase path called for a user table, say appname/users
  • I want to see if the user exists (Fred)
  • I get the link: var users = new Firebase('appname/users')

But I can’t determine if the child exists from here. So here is what I have now:

 users.child('Fred').once('value', function(snapshot) { /** waits around forever if Fred doesn't exist */ }); 

But that does not work. So, now I have to get the value of users (who feel a little intuitive since I am not interested in users) with something like this:

 var users = new Firebase('http://.../appname/users'); users.once('value', function(snapshot) { snapshot.childExists('Fred', function(exists) { /* do something here*/ }); }); 

I don't think I'm having a lot of overhead when fetching document-based appname/users , but it seems like an ugly bit of code if I just want to determine if the "Fred" key exists.

I would like to see something like:

 var users = new Firebase('http://.../appname/users'); users.hasChild('Fred', function(exists[, snapshotOfFred]) { /* do something here*/ }); 

Is there a better approach to using forEach / hasChild? Did I miss any important logical considerations here?

+8
firebase
source share
1 answer

You were on the right track with your first code snippet..once () will call your snapshot callback, whether Fred exists or not. So you can just do:

 users.child('Fred').once('value', function(snapshot) { if (snapshot.val() === null) { /* There is no user 'Fred'! */ } else { /* User 'Fred' exists. } }); 

The second piece of code will actually entail a lot of overhead because Firebase will extract all the “user locations” from Firebase, which can be quite large if you have 1000 users! Thus, the first piece of code is definitely suitable for the transition.

The reason that hasChild and forEach are in DataSnapshot rather than Firebase is because they can be synchronous. In our early testing of the API, we had a combination of synchronous and asynchronous methods available via the Firebase link, but we found that it was a significant stumbling block for people (if people see the hasChild method, they expect it to return a boolean immediately). So we created .on () and .once () to be the only and only asynchronous callback method in Firebase. Everything else in the Firebase link is synchronous (although sometimes we provide optional asynchronous completion callbacks), and everything contained in the DataSnapshot is 100% synchronous.

So our goal was to simplify the asynchronous nature of Firebase for people to understand and use! But perhaps we were not 100% successful. :-) We will take your feedback into account when planning future API settings. Thanks!

+12
source share

All Articles