Search code examples
angularhttpsangular6angular-servicessubscribe

Angular subscribe value is always given the value - 1, the value before the real one


I'm trying to execute an event after the .subscribe is done beceau the code after it depends of the result

verifBordereauExistBase(id: string) {
    return this._BourdereauService.get_one(id).subscribe(
        data = > {
            if (data.idBourdereau == null) {
                this.idBourdereauIsValid = false;
            } else {
                this.idBourdereauIsValid = true;
            }
        }, err = > {
            console.error(err)
        }, () = > {})
}

and the main test is here

AddBordereautoList() {
  this.verifBordereauExistBase(this.idbordereaux);
  console.log(this.idBourdereauIsValid)
  if (Number.isNaN(Number(this.idbordereaux))) {
      this.notifier.notify('error', 'Format code a bare invalide');
  } else if (this.idbordereaux.length != 10) {
      this.notifier.notify('error', 'Format code a bare invalide');
  } else if (this.idBourdereauIsValid == false) {
      this.notifier.notify('error', 'Bordereau n\'existe pas ');
  } else {
      if (this.map.size == 0) {
          this.map.set(1, this.idbordereaux);
      } else {
          let x: number = this.verifexistbordereau(this.idbordereaux);
          if (x === 0) {
              this.map.set(this.map.size + 1, this.idbordereaux);
          } else {
              this.notifier.notify('error', 'Bordereau N°' + this.idbordereaux + 'existe deja dans la liste!');
          }
      }
  }
  this.idbordereaux = "";
}

i'm executing this code and the value of this.idBourdereauIsValid is one step behind, it gives me the value - 1 always


Solution

  • The code under this.verifBordereauExistBase(this.idbordereaux); might be executing before this.verifBordereauExistBase(this.idbordereaux); has a chance to return. To ensure it does, add the AddBordereautoList() code to the .subscribe complete step:

    verifBordereauExistBase(id: string) {
        return this._BourdereauService.get_one(id).subscribe(
            data = > {
                if (data.idBourdereau == null) {
                    this.idBourdereauIsValid = false;
                } else {
                    this.idBourdereauIsValid = true;
                }
            }, err = > {
                console.error(err)
            }, () = > {
                console.log(this.idBourdereauIsValid)
                if (Number.isNaN(Number(this.idbordereaux))) {
                    this.notifier.notify('error', 'Format code a bare invalide');
                } else if (this.idbordereaux.length != 10) {
                    this.notifier.notify('error', 'Format code a bare invalide');
                } else if (this.idBourdereauIsValid == false) {
                    this.notifier.notify('error', 'Bordereau n\'existe pas ');
                } else {
                    if (this.map.size == 0) {
                        this.map.set(1, this.idbordereaux);
                    } else {
                        let x: number = this.verifexistbordereau(this.idbordereaux);
                        if (x === 0) {
                            this.map.set(this.map.size + 1, this.idbordereaux);
                        } else {
                            this.notifier.notify('error', 'Bordereau N°' + this.idbordereaux + 'existe deja dans la liste!');
                        }
                    }
                }
                this.idbordereaux = "";
            })
    }