Home > Software design >  console.log returning undefined on post request
console.log returning undefined on post request

Time:03-19

the function returns a json, but when I give a console.log(this.metodo) the console returns with undefined, the function is in the ngOnit of the application, I don't know what I could be doing wrong, in the code I tried with the promise and with subscribe

  public metodo:any;



ngOnInit(): void {
/*     
 this.cartS.getTransporteMetodo(this.storeS.layout.emp.id).toPromise().then(res=> 
{JSON.stringify(console.log(res))})
*/
 this.cartS.getTransporteMetodo(this.storeS.layout.emp.id).subscribe((res) => {
  this.metodo = res;
  console.log(this.metodo);
});
}

The service

getTransporteMetodo(empId){
return this.http.post(environment.API_URL   'transporte/getAll', { empId });
 }

The json

[
 {
"id": 1,
"emp_id": 1,
"nome": "Retirada na Loja",
"tipo": "RETIRA",
"subtipo": null,
"latLng": [-25.45264, -49.26653],
"vFreteMin": 0,
"vFreteGratis": null,
"periodos": [
  {
    "id": 8,
    "transporte_id": 1,
    "ativo": 1,
    "periodo": "Comercial (das 8h \u00e0s 19h)",
    "corte": "17:00",
    "data": null,
    "week": []
  }
]
 },
]

CodePudding user response:

Try typing this in your service.ts file

getTransporteMetodo(empId): Observable<any> {
   return this.http.post<any>(environment.API_URL   'transporte/getAll', { empId 
   });
 }

You'll need this import in your service

import { Observable } from 'rxjs';

After that check if in your console still appears undefined. Let me know it worked for you

  • Related