2017-03-18 30 views
0

如何使用React设置Firebase数据库顶级的值。使用React设置Firebase数据库顶级的值

一个教程(付费墙后面)解释了如何在子记录中设置一个值,但我还没有弄清楚如何在顶层执行该操作。

更新子记录:

componentDidMount: function() { 
    this.ref = new Firebase('<url>'); 
    var childRef = this.ref.child(this.props.params.username); 
    this.bindAsArray(childRef, 'notes'); 
}, 
handleAddNote: function(newNote) { 
    this.ref.child(this.props.params.username) 
    .child(this.state.notes.length).set(newNote); 
}... 

试图做一些像下面,但不知道如何得到物品的长度在顶层。

componentDidMount: function() { 
    this.ref = new Firebase('<url>'); 
    this.bindAsArray(this.ref, 'notes'); 
}, 
handleAddNote: function(newNote) { 
    this.ref.child(this.state.notes.length).set(newNote); 
}... 

我几天前开始反应,所以任何建议将不胜感激。

回答

0

解决方案:

它先前不工作由于计数元件的长度为没有的元件的特定状态,从而覆盖第一项(索引0)在火力地堡分贝每一次的东西加入。

这工作:

componentDidMount: function() { 
    this.ref = new Firebase('<url>'); 
    this.bindAsArray(this.ref, 'notes'); 
}, 
handleAddNote: function(newNote) { 
    this.ref.child(this.state.notes.length).set(newNote); 
}, 
相关问题