la funcion devuelve un json, pero cuando doy un console.log(this.metodo) la consola devuelve undefined, la funcion esta en el ngOnit de la aplicacion, no se que puedo estar haciendo mal, en el codigo Probé con la promesa y con suscribirse
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); }); }El servicio
getTransporteMetodo(empId){ return this.http.post(environment.API_URL + 'transporte/getAll', { empId }); }el 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": [] } ] }, ]Intente escribir esto en su archivo service.ts
getTransporteMetodo(empId): Observable<any> { return this.http.post<any>(environment.API_URL + 'transporte/getAll', { empId }); }Necesitarás esta importación en tu servicio
import { Observable } from 'rxjs';Después de eso, verifique si en su consola aún aparece sin definir. Déjame saber que funcionó para ti