ReactJS - firebase如何使用特定的.key查询记录

时间:2016-04-11 17:50:09

标签: reactjs firebase

我这里有一个我要从firebase中检索的对象的屏幕截图。

enter image description here

我就是这样做的

table

我得到了这个回复

enter image description here

这不是我想要的,我期待这样的事情

constructor(){
    super();
    this.state = {
        items: []
    };
}
componentWillMount(){
    this.firebaseRef = new Firebase('https://sweltering-heat-7923.firebaseio.com/contact/-KF1XUgOvcrybku8Q52s');
    this.firebaseRef.once("value", function (snapshot){

        snapshot.forEach(function(data){
            console.log(data.val());
        }.bind(this));
    }.bind(this));
}

你会怎么做?

1 个答案:

答案 0 :(得分:2)

您正在迭代contact/-KF1XUgOvcrybku8Q52s的所有子节点并逐个记录它们。要获取完整对象,请使用snapshot.val()代替snapshot.forEach(...)

this.firebaseRef.once('value', function(snapshot){
    console.log(snapshot.val());
});