Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't get the value of a variable declared from another function in angular

I am trying to access the value of the variable existResults declared in a function before the one I am trying to call that variable, but I get an undefined value

  public existResults: any;

  ngOnInit(): void {
    this._activatedRoute.params.subscribe(params => {
      this.patientId = params['patient'];
      const testId = params['test'];
      this.getTestResults();
      this.getProccessesByTest(testId);
    });
  }

  getTestResults(id:any) {
    this._testsService.getTestResults(this.token, id, this.patientId).subscribe(
      response => {
        this.results = response.data;
        if (this.results.length == 0) {
          this.existResults = false;
        }else if(this.results.length > 0) {
          this.existResults = true;
        }
        console.log(this.existResults); //output true
      },
      error => {
        console.log(error);
      }
    );
  }

  getProccessesByTest() {
    console.log(this.existResults); //output undefined
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try this..
ngOnInit(): void {
this._activatedRoute.params.subscribe(params => {
    this.patientId = params['patient'];
    const testId = params['test'];
    this.getTestResults();
});
}

getTestResults(id: any) {
this._testsService.getTestResults(this.token, id,
    this.patientId).subscribe(
        response => {
            this.results = response.data;
            if (this.results.length == 0) {
                this.existResults = false;
            } else if (this.results.length > 0) {
                this.existResults = true;
            }

            this.getProccessesByTest();
            console.log(this.existResults); //output true
        },
        error => {
            console.log(error);
        }
    );
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading