user5946061
user5946061

Reputation:

resolveLocalFileSystemURL TypeError: Cannot read property 'then' of undefined TypeError

I am working on ionic 4 ( capacitor ). I am trying to resolve a local file system URL so I used this function resolveLocalFilesystemUrl but unfortunately, I am getting this error

TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined

This is my code: constructor(private _file: NativeFile) {} ... .. this._file.resolveLocalFilesystemUrl(nativeFilePath).then((entry: Entry) => { console.log(entry); });

Note that this function returns a promise

Upvotes: 0

Views: 1634

Answers (3)

Kevin Bassa
Kevin Bassa

Reputation: 213

If you get this error message:

TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined

The plugin probably does not have permission to access the storage, resulting in all functions and properties returning undefined.

Have you added the needed permission to config.xml?

<preference name="AndroidPersistentFileLocation" value="Compatibility" />

Upvotes: 0

Arnaud L.
Arnaud L.

Reputation: 1

I had the same problem but I found the solution! Have you installed the ionic native file plugin? I dont know capacitor but with cordova : ionic cordova plugin add cordova-plugin-file

Upvotes: 0

Gabriel Bianconi
Gabriel Bianconi

Reputation: 100

Be sure that you are using the right plugin. This is the one you need: https://ionicframework.com/docs/native/file

import { File } from '@ionic-native/file/ngx';

...

constructor(private _file: File) { }

this._file.resolveLocalFilesystemUrl(nativeFilePath).then((entry: Entry) => {
   console.log(entry);
});

Edit

Are you sure the method returns a promise? According to some documentation, the method accepts a callbakc function. So your code will be something like:

this._file.resolveLocalFilesystemUrl(
  nativeFilePath,
  (entry: Entry) => console.log(entry),
  err => console.log(err)
);

EDIT 2

I am working on a cordova based project (Ionic 4) an I am using the officially suggested plugin: https://ionicframework.com/docs/native/file . It is also supported in capacitor projects. Form the official documentation:

You also need a FileEntry object to read an existing file. Use the file property of FileEntry to get the file reference, and then create a new FileReader object. You can use methods like readAsText to start the read operation. When the read operation is complete, this.result stores the result of the read operation.

function readFile(fileEntry) {
  fileEntry.file(function (file) {
    var reader = new FileReader();

    reader.onloadend = function() {
      console.log("Successful file read: " + this.result);
      displayFileData(fileEntry.fullPath + ": " + this.result);
    };

    reader.readAsText(file);

  }, onErrorReadFile);
}

Official Ionic Documentation Official plugin documentation

Upvotes: 1

Related Questions