简体   繁体   中英

how can i check if an existing node in firebase using angularfire2?

im checking to see if a node exists in firebase if my function returns false i use the push method if true i will use the update method. this is my function it takes the 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;
  }

the first time i push the button it creates a new node even if the node exists the second time i push the button it updates the node correctly. why is this happening every time i refresh the page and i push the save button it creates a new node but the second time it updates perfectly.

AskFirebase

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM