Problem while getting Promise return

I’m in a firebase ionic project, and I’m facing some problems. I’m newbie in mobile development, so my problem is when getting a return of a update command in firebase. In my provider I have:

private clientCollection: AngularFirestoreCollection<Client>;
constructor(
    public db: AngularFirestore
  ) {
    this.clientCollection = db.collection<Client>('clients');
    this.historicoCollection = db.collection<Historico>('historico');
  }

And my function after, receive a Client Object and a key of firebase register:

updateUser(user: any, key: string){
    return this.clientCollection.doc<Client>(key).update(user)
  }

My scan function, calls updateUser in provider:

saveUser(data: Client){
      this.meuLoader.dismiss();
    
      this.db.getUserPassword(data.password).subscribe((res) => {
        if(res.length){
          data.key = res[0].key;
          //this.goToResult(data);
        }
      });         

        this.db.updateUser(data, data.key).then((res) => {
          this.goToResult(data);
        }).catch((error) => {
            this.alertCtrl.create({
              title: 'retorno',
              subTitle: 'Erro no retorno da promisse',
              buttons: ['Dismiss']
            }).present();
        })      
  }

The saveUser function calls two functions in provider. Is there a problem making this way? When I run on my device, nothing works. The node does not update…

Thanks

It’s most likely a async problem. What’s there inside goToResult(data)?

goToResult(object: Client) {
this.modalCtrl.create(DetailPage, {data: object}).present();
}

It opens a model, that contains many items. It’s not the problem, if I cut some piece og code, it works. The problem is that I can’t update the firebase node, as i showed early.

Try and see if the below works.

this.db.getUserPassword(data.password).subscribe((res) => {
  if(res.length){
    data.key = res[0].key;
    this.db.updateUser(data, data.key).then((res) => {
      this.goToResult(data);
    }).catch((error) => {
        this.alertCtrl.create({
          title: 'retorno',
          subTitle: 'Erro no retorno da promisse',
          buttons: ['Dismiss']
        }).present();
    });      
  }
});         

It works for me! Thanks a lot.

1 Like

Can you accept that as a solution?

Thanks a lot but I mean the one with code I have written.