My task is to recursively read all files from a directory.
Below is the code snippet I have used:
import { Component } from '@angular/core';
import { File } from '@ionic-native/file'
@Component({
selector: 'page-files',
templateUrl: 'files.html',
})
export class FilesPage {
public filePaths: string[];
constructor(private file: File) {
}
ionViewDidLoad() {
this.findFiles("WhatsApp/Media");
}
findFiles(dir: string): void {
this.file.listDir(this.file.externalRootDirectory, dir).then(
(files) => {
for (let file of files) {
if (file.isDirectory && file.name != '.' && file.name != '..') {
this.findFiles(dir + "/" + file.name)
} else {
this.filePaths.push(file.name)
}
}
}
).catch(
(err) => {
console.log(err.toString())
}
);
}
}
While running the code, I encountered a run time error "this.filePaths is undefined" at this.filePaths.push(file.name)
Even though the member function
this.findFiles(dir: string): void
is present, it seems like something is happening with the this.filePaths
member variable.