【问题标题】:I can´t get access to the values of an javascript object retrieved from firebase我无法访问从 firebase 检索到的 javascript 对象的值
【发布时间】:2016-02-12 02:11:38
【问题描述】:

嗨,我已经尝试了很多方法来获得它,但我迷路了。

这是我的功能:

var ref = new Firebase("https://<my-url>.firebaseio.com/");
    var ref2 = ref.orderByChild("matricula").equalTo(matricula);
    var ref3 = ref2.on("value", function(snapshot){

        console.log(snapshot.numChildren());
        console.log(snapshot.val());
        }

检索到的对象是:

Object {-KACGpGoGlVbgbHM4ibI: Object}
-KACGpGoGlVbgbHM4ibI: Object
carrera: "LAMX54"
comentarios: " "
curp: "rffgr4445454445"
matricula: "006843"
nombre: "JOSE GERARDO LOPEZ VILLARREAL"
postulado: "no"
tetra: "7"
__proto__: Object
__proto__: Object

好的,例如,当我尝试获取 snapshot.val().carrera 时,响应为“未定义”,即使我尝试获取 snapshot.key() 响应为“null”,我如何才能获取里面的值对象?,提前谢谢!!!!!

【问题讨论】:

    标签: firebase


    【解决方案1】:

    当您执行查询时,您可能有许多子节点匹配条件。因此,您的 value 事件将由一组子节点触发。即使只有一个孩子,它仍然会在一个集合中。

    要处理这个问题,你需要遍历孩子:

    var ref = new Firebase("https://<my-url>.firebaseio.com/");
    var ref2 = ref.orderByChild("matricula").equalTo(matricula);
    var ref3 = ref2.on("value", function(parent){
        console.log(parent.numChildren());
        console.log(parent.val());
        parent.forEach(function(snapshot) {
            console.log(snapshot.val().carrera);
        });
    });
    

    【讨论】:

    • 非常感谢,我正在学习 angular 和 firebase 实际上我有一个新帖子,我认为这个帖子更复杂 :( ,如果你能检查一下那就太好了!在这里:@ 987654321@
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多