Array Scope

Hi
Can anybody please learn me why this.selectedPage[0].PageText; is working in promise but is not accessible out of it !? the array is not empty.

Runtime Error: Uncaught (in promise): TypeError: Cannot read property ‘0’ of undefined

  LoadPage(index: number){

      this.db.getStoryPage(this.selectedStory.st_ID,index).then((pagedata)=>{
          this.selectedPage = [];
          this.selectedPage = pagedata;
//is working here
          this.PageText = this.selectedPage[0].PageText;

      }).catch((err)=>{console.error(err)});

//is not accessible here:
      this.PageText = this.selectedPage[0].PageText;
  }

Your code inside the then block is executed when an async operation is completed, so when you try to access the
this.selectedPage it is still undefined. More about promises:

2 Likes