Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70429

Functions return value but array length are returning 0

$
0
0

@bobyui wrote:

Hi,

I created 2 functions for multiple file uploads where I need to get the ID of the file after the file upload then save the id into array and process it later on, but the data returned the value but array length always show 0.

   upload() {

    const files = this.fileField.getFiles();
    const formData = new FormData();

    return new Promise( (resolve, reject) => {
      return this.storage.get('authtoken').then((authkey) => {
        files.forEach((file) => {
          formData.append('files[]', file.rawFile, file.name);
          this.http.post(
            this.authService.AUTH_SERVER_LIVE + '/file/upload', file.rawFile, {
              headers: {
                'Content-Disposition': 'file; filename="' + file.name + '"',
                Authorization: 'Bearer ' + authkey,
                'Content-Type': 'application/octet-stream' }})
            .pipe(catchError(e => this.handleError(e))).subscribe((filesData: any) => {
              console.log('files uploaded', filesData);
              this.fileFids.push({id: filesData.fid[0].value});
            });
        });
        resolve(this.fileFids);
      });
    });

  }

and I run it using this code :

   async runcode() {
    await this.upload().then((fids: any) => {
      console.log('uploaded:', fids);
      console.log(Array.isArray(fids));
      console.log(Object.keys(fids).length);
      this.callanother(fids);
    });
  }

but the results are showing, but the array length are always returned 0 even though I can see the length in console is not 0.

and also the console in the runcode() function shows 1st before the console log in the upload code :

uploaded: []0: {id: 3377}id: 3377__proto__: Object1: {id: 3378}length: 2__proto__: Array(0)
form.page.ts:109 true
form.page.ts:110 0
form.page.ts:336 files uploaded {id: Array(1)]__proto__: Object
form.page.ts:336 files uploaded {id: Array(1)]__proto__: Object

is there anything that I am missing?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70429

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>