I need to read all the files recursively from a directory.
I used the code snippet:
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())
}
);
}
}
I am getting run time error "this.filePaths is undefined" at this.filePaths.push(file.name)
However member function this.findFiles(dir: string): void
is available, do not what happen to this.filePaths
member variable.