我想要更新我的firebase dB记录。因为我将播放器对象绑定到我不想在提交更新之前刷新的UI组件,所以我制作了真实firebase记录的克隆副本,如下所示:
newPlayerWithSameKey(p: Player): any {
const origKey = (<any>p).$key;
const duplicate = JSON.parse(JSON.stringify(p))
duplicate.$key = (<any>p).$key;
return duplicate;
}
如果我在控制台中记录此内容,则p
和duplicate
完全相同。
然后将副本注入到处理记录的数据服务的update方法中。我试图以这种方式更新它:
updatePlayer(player: Player): void {
const key = this.getKey(player);
// The following three lines are just for the console log to demonstrate
let shouldBeThePlayerToBeUpdated;
this.db.object('/players/' + key).subscribe(p => shouldBeThePlayerToBeUpdated = p);
window.console.log('Mock player: ', key, JSON.stringify(shouldBeThePlayerToBeUpdated));
window.console.log('Updating player with key: ', key, JSON.stringify(player));
this.db.list('/players').update(key, player)
}
private getKey(p: Player): string {
return (<any>p).$key
}
但是我收到以下错误:正如您可以看到密钥完全匹配,并且firstName属性正在正确更新(这就是我在表单中所做的)。我真的很困惑这里的问题是什么......有人有想法吗?
答案 0 :(得分:0)
您可以替换:
this.db.list('/players').update(key, player)
通过
this.db.object('/players/'+key).update(player)
编辑:
this.db.object('/players').update({'/'+key, player})
一个例子:我如何使用它:
const update = {
'starsNumber': someValue + 1
}
update[`level/${photo_id}/photo_id`] = photo_id
}
firebase
.database()
.ref(`game/users/${admin_id}/players/${user.id}/`)
.update(update)