控制台日志未定义

问题描述 投票:-1回答:2

控制台日志在这里未定义

      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);
  }  

如何将结果分配给Result Object

angular
2个回答
0
投票

thenpromise中记录您的数据:

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);
  });

0
投票

你在等待吗?

你应该做这样的事情:

const main = async () => {
await this.http.get(this.rootURL + "/Report/"+id).toPromise().then(data => {
console.log(data);
})
}
© www.soinside.com 2019 - 2024. All rights reserved.