reactfire React + firebase“在属性中包含无效密钥(.key)”

时间:2016-03-22 07:24:15

标签: reactjs firebase reactfire

尝试使用update()或set()

时出错

我的相关代码:

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

1 个答案:

答案 0 :(得分:1)

我能够通过硬编码新的状态对象来实现我的预期结果,基本上省略了&#39; .key&#39; ...必须有更好的方法。接受建议。谢谢!

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