Home > Mobile >  Loop not generated when using promise in angular
Loop not generated when using promise in angular

Time:11-30

I have this function

handleFileInput(file: any) {  
      let promise = new Promise((resolve, reject) => {
      this.uploadFileDetails.push({ filename:this.FileName,filetype:this.FileType});
      ...
       resolve(data.Location);
      return promise;
}

when i loop through this.uploadFileDetails i get error

ERROR TypeError: Cannot read properties of undefined (reading 'push')

   <div *ngFor="let fileDetails of uploadFileDetails">

    </div>

but when i remove this loop then i can see the values being saved properly.

Any solution Thanks

CodePudding user response:

Could you please try this solution, I guess issues with scope of variables in Promise respons

handleFileInput(file: any) { 
    var that = this; <--- add this line
    let promise = new Promise((resolve, reject) => {
        that.uploadFileDetails.push({ filename:that.FileName,filetype:that.FileType});
        ...
        resolve(data.Location);
        return promise;        
    }
}
  • Related