-1

console log undefined here

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

here console log the data

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

what can I do to assign result to Result Object

  • You need to perform your other operations inside the `then` callback, or use async/await. –  Apr 20 '19 at 11:50
  • Possible high level duplicate https://stackoverflow.com/questions/39458201/understanding-javascript-promise-object –  Apr 20 '19 at 11:51

2 Answers2

0

Log your data within the then of your promise:

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);
  });
pzaenger
  • 11,381
  • 3
  • 45
  • 46
0

are you using await?

you should be doing something like this:

const main = async () => {
await this.http.get(this.rootURL + "/Report/"+id).toPromise().then(data => {
console.log(data);
})
}
Liam Seskis
  • 161
  • 1
  • 2
  • 10