Unable to get promise from File Reader in Ionic 5 Capacitor

Below is my code - The problem comes when i try to call edit()

edit () {
    this.startUpload().then(data => {
        let that = this;
        this.promisearr = [];
        data.forEach(function (d) {
            (<FileEntry>d).file(_file => {
                console.debug(_file);
                that.promisearr.push(that.readFile(_file));
            })
        })


        Promise.all(this.promisearr).then(d => {
            //I post the data here. but d=[] ?? what's wrong
        });
    });
}

readFile = (file) => {
    let reader = this.getFileReader();

    return new Promise((resolve, reject) => {
        reader.onerror = () => {
            reader.abort();
            console.error("Error in Reading File");
        };

        reader.onload = () => {
            const imgBlob = new Blob([reader.result], {
                type: file.type
            });
            this.formData.append('vehicle_rc_copy', imgBlob, file.name);
            resolve(reader.result);
        };

        reader.onloadstart = () => {
        };

        reader.readAsArrayBuffer(file);
    })
}


startUpload() {
    let promise_arr = [];
    for (let index = 0; index < this.images.length; index++) {
        const element = this.images[index];
        promise_arr.push(
            this.file.resolveLocalFilesystemUrl(this.images[index].filePath)
            // .then(entry => {
            //     (<FileEntry>entry).file(async file => {
            //         await this.readFile(file)
            //     })
            // })
            // .catch(err => {
            //     this.presentToast('Error while reading file.');
            // })
        );
    }

    return Promise.all(promise_arr);
}

Some time i get d = [ ] when testing on android phone, but on chrome inspect if i have some breakpoints i get the d with some value.

can you please help in understanding what am i doing wrong ?

1 Like