如何使用angularfire2检查firebase中的现有节点?

时间:2017-09-23 12:27:31

标签: node.js angular firebase angularfire2

如果我的函数返回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;
  }

第一次按下按钮,即使节点存在,它也会创建一个新节点 第二次按下按钮,它正确更新节点。 为什么会发生这种情况 每次刷新页面并按下保存按钮,它都会创建一个新节点 但第二次完美更新。

AskFirebase

1 个答案:

答案 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
            }
        })
}