Angular6 httpclient has already assigned a value to the field, why does the field always undefined?

has been executed to the yellow part, res ["data"] .itemMaterialType = 3,
this.itemMaterialType= res [" data"] .itemMaterialType; this sentence has been executed, why this.itemMaterialType has always been the theoretical this.itemMaterialType=3 of undefined;.

the following is the encapsulation method,

  async searchData() {
    this.itemMaterialType = "4";
    await this.api.Dispatch.queryProcessDetailByProcessId({
      processId: this.processId
    })
      .then(res => {
        this.data = res["data"];
        this.isFeedMaterial = res["data"].isFeedMaterial.toString();
        this.itemMaterialType = res["data"].itemMaterialType;
      });
  }
    queryProcessDetailByProcessId: async (option) => await this.http.getByPromise("/api/ScheduleProcess/GetProcessDetail", option),
  async getByPromise(url: string, params: {}) {
    return await this.http.get(this.domain + url, { observe: "body", params: params })
      .toPromise();
  }
The

this context has changed. You can print out the this and see where it points.


Why do you bring then,


when you use sync await,?

1. Then after getting the data, there is no need to use async await

2 after obtaining the res, you can directly use the abbreviation to use the dot syntax res.data

.

3 resolve after conversion to promise in api (data) reject (cache Error) does not need to be forced to be converted to Synchronize


browser illusion that what is printed on the console is not necessarily real. Sometimes bug, appears in this, but there is no problem with using it in the code. If there is a problem when using the code, you can find the reason again


VS code debugging status this has always been undefined, but the data that is running normally can also be displayed; debugging with the browser found that this is also normal; pit

Menu