如果我的函数返回false,我会检查firebase中是否存在节点 我使用push方法如果是,我将使用更新方法。 这是我的功能它需要id:
checkIfExist(path:string){
var a;
const user = this.db.object(`/users/${path}/`,{ preserveSnapshot: true });
user.subscribe((obj) => {
if (obj.$exists()) {
a = true;
} else {
a = false;
}
});
return a;
}
第一次按下按钮,即使节点存在,它也会创建一个新节点 第二次按下按钮,它正确更新节点。 为什么会发生这种情况 每次刷新页面并按下保存按钮,它都会创建一个新节点 但第二次完美更新。
答案 0 :(得分:0)
试试这个
saveData(){
this.db.object(`/users/YourPath/`,{ preserveSnapshot: true })
.subscribe(snapshot => {
if(snapshot.exists()) {
//data already there, here goes your update method
}else{
//no data exists, here goes your push method
}
})
}