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?
firebase
Kato
source share