With my limited understanding, and SQL background, I'm not quite groking the usage of hasChild() and forEach(); they feel like they belong on the reference and not the snapshot.
(I'll use hasChild() for the rest of the discussion, but the concepts are interchangeable.)
Here's my reasoning:
- I have a Firebase path called for a users table, say
appname/users
- I want to see if a user (Fred) exists
- I obtain a reference:
var users = new Firebase('appname/users')
But I can't determine if a 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 doesn't quite work. So now I must get the value of users (which feels a bit counter-intuitive, since I'm 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 imagine I incur a large overhead by fetching appname/users
, based on the docs, but this feels like an unsightly bit of code if I just want to determine if the key 'Fred' exists.
I'd 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? Have I missed any important logical considerations here?