problem with http get angular 5

0

The problem is that the method is returning me undefinded the code is

    getLugares() {
    this._http.get(this.url + 'lugar')  //usar la url que corresponda
      .map(res => res)
      .subscribe(
        res => this.lugares=res,
        err => console.error(err),
        () => console.log('Funciona!')
      );
  }

It is not a problem of the web service or the URL because I already tried them. Also when changing line 5 so res => console.log(res), in the log the array appears correctly

    
asked by Matthew Seidel 07.04.2018 в 19:21
source

1 answer

1

The problem seems to be this.lugares , in that scope this no longer refers to your original object, so using a variable that can help you:

 getLugares() {
    const that = this
    that._http.get(that.url + 'lugar')  //usar la url que corresponda
      .map(res => res)
      .subscribe(
        res => that.lugares=res,
        err => console.error(err),
        () => console.log('Funciona!')
      );
  }
    
answered by 07.04.2018 / 19:29
source