簡體   English   中英

console.log 在發布請求時返回 undefined

[英]console.log returning undefined on post request

function 返回 json,但是當我給出 console.log(this.metodo) 時,控制台返回 undefined,function 在應用程序的 ngOnit 中,我不知道我在代碼中做錯了什么我嘗試使用 promise 並訂閱

  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';

之后檢查您的控制台中是否仍然顯示未定義。 讓我知道它對你有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM