2016-03-22 26 views
1

我在尝试使用update()或设置()当错误reactfire阵营+火力 “包含属性无效键(.KEY)”

我的相关代码:

const ref = new Firebase('https://whatever.firebase.org/employees'); 
export default class EmployeeNew extends React.Component { 
... 
    this.update =() => { 
    console.log(this.state, this.props.employee['.key']); 
    ref.child(this.props.employee['.key']).set(this.state); // <-- no-dice 
    // ref.update(this.state); <-- also fails. 
    } 
... 
} 

输出

Object {avatar: "https://somevalue.png", name: "Bananaman", .key: "-KDObp8r82Ornrrmfbk5"} 
Object "-KDObp8r82Ornrrmfbk5" 

错误浏览器控制台:

app.js:28366 Uncaught Error: Firebase.set failed: First argument contains an invalid key (.key) in property 'employees.-KDObp8r82Ornrrmfbk5'. Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]" 
控制台日志的,如上述写入时被调用更新个

东西我曾尝试:

ref.update(this.state); // same error 
    ref.update({this.props.employee['.key']: this.state}); // same error 
+0

我能够通过对新状态对象进行硬编码,实质上省略'.key'来实现我的预期结果......必须有更好的方法。 – Pandem1c

回答

1

我能够通过硬编码新状态对象,基本上是忽略“关键”,实现我想要的结果......必须有一个更好的办法寿。接受建议。谢谢!

this.update =() => { 
    let updatedEmployee = {avatar: this.state.avatar, name: this.state.name}; 
    ref.child(this.props.employee['.key']).set(updatedEmployee); 
} 
+0

我不确定是否有更好的方法 - 我很难看出为什么React Fire是这样设计的。无论如何,是'this.state.name'和'this.state.avatar'对象或只是在firebase中的简单值?因为如果它们是值,你应该做'avatar:this.state.avatar ['。value']'。但我想你知道这一点。 – ArneHugo