I'm using angularFire 0.8.2 and I'm trying to check a uniqueness of an user.
This user is stored in users/[id] where [id] is the simpleLogin generated id for login/password users and social uid for one that comes from social networks (i.e: facebook:891239120).
So when I'm logging with facebook, I need to check if that user is already in firebase, so I'm using (from angularFire-seed):
var userData = fbutil.syncObject('users/' + user.uid);
userData.$loaded()
.then(function (data) {
if(data) {
console.log('EXISTS');
console.log(data.$id);
} else {
console.log('NOT EXISTS');
}
}, function (err) {
console.error(err);
});
But this not work because the data object is always returned (even if the object not with key user.uid not exists)
I found that obj.$value is filled with null when the obj is empty, and not present when the obj has not primitive value, but I'm not sure if this is the correct way to check it.
And I also don't know if I use $asArray on /users it will load EVERY user on firebase than in production it could have performance problems?
When working with primitive values (which null is one of), AngularFire stores them in the $value key. Thus, to test if the data at the path exists, you can simply perform:
data.$loaded(function() {
var dataExists = data.$value !== null;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With