控制台日志未定义

时间:2019-04-20 11:48:56

标签: angular

此处未定义控制台日志

      this.http.get(this.rootURL + "/Report/"+id)
      .toPromise().then(res => this.report = res as Report);
      console.log(this.report);
  }  

此处控制台记录数据

      this.http.get(this.rootURL + "/Report/"+id)
      .toPromise().then(res => console.log(res));
      console.log(this.report);
  }  

我该怎么做才能将结果分配给结果对象

2 个答案:

答案 0 :(得分:0)

promisethen中记录数据:

this.http.get(this.rootURL + "/Report/"+id)
  .toPromise()
  .then(res => {
    // Promise gets fulfilled and its response can be assigned
    this.report = res as Report;
    console.log(this.report);

    // Perform other actions
    // ...
  })
  .catch(reason => {
    // Something went wrong, your promise gets rejected
    console.log(reason);
  });

答案 1 :(得分:0)

您在使用等待吗?

您应该这样做:

const main = async () => {
await this.http.get(this.rootURL + "/Report/"+id).toPromise().then(data => {
console.log(data);
})
}