0
votes

I know that has a lot of questions about this, but i tried a lot of tips and the problem persists.

i implement a get that return an observable:

public getEstatisticasPara(motorista:Motorista): Observable<EstatisticaAvaliacao> {
    let params = new URLSearchParams();
    params.set(Config.ESTATISTICA_AVALIACAO_REQUEST_PARAM, motorista.getId().toString());

    let options = new RequestOptions({ headers: this.headers, params: params });

    return this.http.get(this.path, options)
                    .map((res) => res.json) 
                    .catch(this.handleError);
}

When i try to subscribe always get a undefined:

 atualizaMediaAvalicaoes(){
  let motorista:Motorista = FlyweightFactory.getInstanceOf("motorista");
  motorista.setId(1);
  this.estatisticaAvaliacaoProvider.getEstatisticasPara(motorista)
                                   .subscribe((res)=>{
                                        console.log(JSON.stringify(res));
                                        let estatisticaAvaliacao:EstatisticaAvaliacao = EstatisticaAvaliacao.fromJson(res);
                                        this.storage.set("mediaAvaliacao", estatisticaAvaliacao.getMedia());
                                    },(err)=>{
                                        console.log(err);
                                    });
}

Network console show correct response... so why the undefined ? =/

1
Are you using the old Http service or the new HttpClient ? And how does your res object looks like ? Because res.json seems strange. - cyr_x
Http: import { Http} from '@angular/http'; - i think is the old .. the rest response is : {"total":8,"totalCincoEstrelas":2,"totalQuatroEstrelas":3,"totalTresEstrelas":2,"totalDuasEstrelas":1,"totalUmaEstrela":0,"media":3.75} - Rodrigo Sene
Shouldn't this ` .map((res) => res.json)` be this: ` .map((res) => res.json())`. Notice the additional parentheses. - DeborahK
Yep that's what i ment with looks strange :) - cyr_x
lol..... thats it asuhdsuahdhaudhsaud Thanks. - Rodrigo Sene

1 Answers

0
votes

This

.map((res) => res.json)

should be this:

.map((res) => res.json())

Notice the additional parentheses.