console.log在发布请求时返回undefined



函数返回一个json,但当我给出console.log(this.metodo(时,控制台返回未定义,函数在应用程序的ngOnit中,我不知道我可能做错了什么,在我尝试使用promise和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);
});
}

服务

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

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": []
}
]
},
]

尝试在service.ts文件中键入此内容

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

您的服务中需要此导入

import { Observable } from 'rxjs';

之后,检查控制台中是否仍显示未定义。让我知道它对你有效

最新更新