尝试使用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发布于 2016-03-22 15:31:29
我能够通过硬编码新的状态对象来实现我的预期结果,本质上省略了'.key'...不过,肯定有更好的办法。欢迎您的建议。谢谢!
this.update = () => {
let updatedEmployee = {avatar: this.state.avatar, name: this.state.name};
ref.child(this.props.employee['.key']).set(updatedEmployee);
}https://stackoverflow.com/questions/36148382
复制相似问题