0

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.

Anil8753
  • 2,663
  • 4
  • 29
  • 40

1 Answers1

0

filePaths is undefined. Try to instantiate the variable in the constructor like

this.filePaths = [];
cayz
  • 1
  • 2