How to Crawl Unique Identity and Reference Child Nodes

My firbase database looks like this:

app
   users
       -gn4t9u4ut304u9g4
            email
            uid

      

How can I access the email and uid? When I try this:

        $rootScope.dashtype.child('users').orderByChild('uid').equalTo($rootScope.auth.uid).on('value', function(snapshot){
            $rootScope.user = snapshot.val();
            console.log($rootScope.user);
        })

      

I get the correct object, but with a unique id as root:

Object {-JvaZVrWGvJis0AYocBa: Object}

      

And since this is a dynamic property, I don't know how to refer to the child objects. I just want to be able to access user fields like this: $ rootScope.user.email etc.

+1


source to share


1 answer


Since you are asking for a value, the result is a list of users. It may only be one user, but he is still one of them.

You will need to loop over the snapshot to navigate to the child node:

$rootScope.dashtype.child('users').orderByChild('uid').equalTo($rootScope.auth.uid).on('value', function(snapshot){
    snapshot.forEach(function(userSnapshot) {
        $rootScope.user = userSnapshot.val();
        console.log($rootScope.user);
    });
});

      



Since there is only one user in the list, the loop is executed only once.

This is where you mix regular Firebase JavaScript with AngularFire. This means that you will need to tell AngularJS that you have updated the scope in order for it to change the view:

$rootScope.dashtype.child('users').orderByChild('uid').equalTo($rootScope.auth.uid).on('value', function(snapshot){
    snapshot.forEach(function(userSnapshot) {
        $timeout(function() {
            $rootScope.user = userSnapshot.val();
            console.log($rootScope.user);
        });
    });
});

      

+3


source







All Articles