Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用React在Firebase db的顶层设置一个值_Javascript_Reactjs_Firebase Realtime Database - Fatal编程技术网

Javascript 使用React在Firebase db的顶层设置一个值

Javascript 使用React在Firebase db的顶层设置一个值,javascript,reactjs,firebase-realtime-database,Javascript,Reactjs,Firebase Realtime Database,如何使用React在Firebase db的顶层设置值 一个教程(在付费墙后面)解释了如何在子记录中设置一个值,但我还没有弄清楚如何在顶层进行设置 更新子记录: componentDidMount: function() { this.ref = new Firebase('<url>'); var childRef = this.ref.child(this.props.params.username); this.bindAsArray(childRef, 'note

如何使用React在Firebase db的顶层设置值

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

更新子记录:

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);
},
componentDidMount:function(){
this.ref=新火基(“”);
var childRef=this.ref.child(this.props.params.username);
本.bindAsArray(childRef,'notes');
},
handleAddNote:功能(新注释){
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);
}...
componentDidMount:function(){
this.ref=新火基(“”);
this.bindAsArray(this.ref,“notes”);
},
handleAddNote:功能(新注释){
this.ref.child(this.state.notes.length).set(newNote);
}...

我几天前就开始做出反应,因此非常感谢您的建议。

解决方案:

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);
},
以前,由于计算没有元素的特定状态的元素长度,因此每次添加某个元素时,都会覆盖Firebase db中的第一个项目(0索引),因此无法正常工作

这起作用了:

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);
},
componentDidMount:function(){
this.ref=新火基(“”);
this.bindAsArray(this.ref,“notes”);
},
handleAddNote:功能(新注释){
this.ref.child(this.state.notes.length).set(newNote);
},