Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the object exists in Firebase using AngularFire $asObject()?

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?

like image 590
Douglas Correa Avatar asked Dec 11 '22 03:12

Douglas Correa


1 Answers

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;
});
like image 108
Kato Avatar answered Jan 04 '23 22:01

Kato