如何绕过唯一的ID和引用子节点 [英] How to bypass unique ID and reference child nodes

查看:129
本文介绍了如何绕过唯一的ID和引用子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的firbase数据库看起来像这样:

  app 
用户
-gn4t9u4ut304u9g4
电子邮件
uid

如何引用电子邮件和uid?当我尝试这样做:
$ b $ pre $ $ $ c $ $ rootScope.dashtype.child('users')。orderByChild('uid')。equalTo ($ root $ sudo.uid).on('value',function(snapshot){
$ rootScope.user = snapshot.val();
console.log($ rootScope.user);




我得到正确的对象,但是以唯一的id作为根:

 对象{-JvaZVrWGvJis0AYocBa:Object} 

因为这是一个动态属性,我不知道如何引用子对象。我只是想能够访问像这样的用户字段:$ rootScope.user.email等。

解决方案

重新申请一个值,你会得到一个用户列表。它可能只是一个用户,但它仍然是一个列表。



您将不得不循环快照,以达到子节点:


$ b $

  $ 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 );
});
});

由于列表中只有一个用户,所以只执行一次循环。



您正在将常规的Firebase JavaScript与AngularFire混合使用。这意味着你需要通知AngularJS你已经更新了范围,这样它才会重新显示视图:

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


My firbase database looks like this:

app
   users
       -gn4t9u4ut304u9g4
            email
            uid

How do I reference 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 the unique id as root:

Object {-JvaZVrWGvJis0AYocBa: Object}

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

解决方案

Since you're requesting a value, you get a list of users as a result. It may only be one user, but it's still a list of one.

You will have to loop over the snapshot, to get 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's only a single user in the list, the loop for execute just once.

You are mixing regular Firebase JavaScript with AngularFire here. This means that you will need to inform AngularJS that you updated the scope, so that it will rerender 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);
        });
    });
});

这篇关于如何绕过唯一的ID和引用子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆