File.readAsText never resolves

Hi there, I’m currently having an iOS-specific problem where File.readAsText never resolves properly, everything in .then() doesn’t run, not the logs, the variable setting, nor the the function call. This code works fine on android devices to read the file I need to have read. When I give readAsText an error by misspelling the name or getting the path wrong, it does catch it and log the error code and description, but when it tries to read the file that actually exists, it never resolves and doesn’t update the data in the app. The other two File functions I use work fine and log out the fact that the file does, in fact, exist, so I feel like I’m going insane. Can anyone help me with this? Here is the code for reference:
console.log(‘trying to import JSON as plaintext’);

  File.checkFile(this.dataDirectory, "mydir/data.txt").then(res => {
    console.log('File exists:');
    console.log(res);
  }).catch(err => {
    console.log('something is wrong');
    console.log(err);
  });

  File.listDir(this.dataDirectory, "mydir").then(res => {
    console.log('Directory contents:');
    console.log(res);
  }).catch(err => {
    console.log('something is wrong');
    console.log(err);
  });

  File.readAsText(this.dataDirectory, "mydir/data.txt").then(fileStr => {
    console.log(fileStr);
    var fileObj = JSON.parse(String(fileStr));
    console.log(fileObj);
    this.setData(fileObj);
  }).catch(err => {
    console.log('it messed up wow');
    console.log(err);
  });
2 Likes

As it happens, there is a problem within the cordova-file plugin that makes certain promises never resolve in iOS without a fix in zone.js. The fix is to put the polyfills.js script tag before the cordova.js script tag in index.html.

1 Like

I have the same problem on Android. Tried the suggested solution and it didn’t work for me, i.e. still having the same problem. Does anybody have other suggestions how to solve it? Thanks.

Since Cordova file plugin isn’t working for me, I was wondering whether are there other ways to read a file in Ionic.?

Sorry I can’t be any help, but I am also having this exact issue and the suggested solution does not work for me either.

ionic info:

Cordova CLI: 6.5.0
Ionic Framework Version: 2.2.0
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.1.4
ios-deploy version: 1.8.5
ios-sim version: 5.0.6
OS: OS X El Capitan
Node Version: v5.5.0
Xcode version: Xcode 7.3.1 Build version 7D1014

same problem on ios. ionic info :

global packages:

@ionic/cli-utils : 1.0.0
Cordova CLI      : 7.0.1
Ionic CLI        : 3.0.0

local packages:

@ionic/app-scripts              : 1.3.7
@ionic/cli-plugin-cordova       : 1.0.0
@ionic/cli-plugin-ionic-angular : 1.0.0
Ionic Framework                 : ionic-angular 3.2.0

System:

Node       : v6.10.3
OS         : macOS Sierra
Xcode      : Xcode 8.3.1 Build version 8E1000a
ios-deploy : 1.9.0
ios-sim    : 5.0.13

===

Code example :

// works fine
this.file.checkFile(this.file.dataDirectory, 'file.json').then(_ => {

     // Here the promise never resolves
     this.file.readAsText(this.file.dataDirectory, 'file.json').then(text => {}).catch(err => {});
 })

The problem gonna be resolved in next version of ionic-native. See this issue:

Also the PR if you temporary want to fix locally the issue by changing your local code manually:

1 Like

Ok perfect, thank you

I can confirm that this now works in ionic-native 3.8. Thank you much!

1 Like

same problem on ios. ionic info :

global packages:

@ionic/cli-utils : 1.0.0
Cordova CLI      : 7.0.1
Ionic CLI        : 3.0.0

local packages:

@ionic/app-scripts              : 1.3.7
@ionic/cli-plugin-cordova       : 1.0.0
@ionic/cli-plugin-ionic-angular : 1.0.0
Ionic Framework                 : ionic-angular 3.2.0

System:

Node       : v6.10.3
OS         : macOS Sierra
Xcode      : Xcode 8.3.1 Build version 8E1000a
ios-deploy : 1.9.0
ios-sim    : 5.0.13

===

Code example :

// works fine
this.file.checkFile(this.file.dataDirectory, 'file.json').then(_ => {

     // Here the promise never resolves
     this.file.readAsText(this.file.dataDirectory, 'file.json').then(text => {}).catch(err => {});
 })

Thanks @reedrichards.

Another temporary hack to read file is to use http. Works for me.

For the record, this is solved now in ionic-native

@ionic-native/core”: “^3.7.0”,
"@ionic-native/device": “^3.12.1”,
"@ionic-native/file": “^3.12.1”,
"@ionic-native/network": “^3.5.0”,
"@ionic-native/splash-screen": “3.7.0”,
"@ionic-native/status-bar": “3.7.0”,
"@ionic/storage": “2.0.1”,

Having the same issue over here!! readAsText doesn’t resolve.

Maybe try to update core?

Don’t know if that’s the problem but I always try to have to same version for all the ionic-native import (you’ve got a mix between 3.5, 3.7 and 3.12.1…)

done, still getting no response… I have been researching hours about this, looks like this is an old issue in Github, they always kinda try to close the issue without a well-defined solution, then people come back again with the same problem…

Mmmmh well I use ionic-native 4.12.1 and cordova-plugin-file 4.3.3, both last version, and I still don’t face the problem anymore.

Could you remove node_modules and install them again?

rm -r node_modules
npm install

P.S.: For the record, had to push a new version in store, so I tested manually the part where I use ionic-native/file and everything was working like a charm

1 Like

You mean ‘@ionic-native/core: 3.12.1’, right? and yeah, the same over here, I’m reading the source code on the repo for references, could be my OS, people are saying that is working for Mac fellas, But got no idea why isn’t it on Linux. Anyway, thanks man

1 Like

Exactly sorry I meant ‘@ionic-native/core: 3.12.1’

Glad to hear it’s working!

Is not working on Ubuntu, … I just used Cordova’s fileReader in my project, this wrapper is not working, will try to find out why and submit the issue, later… thanks again

I mean isn’t the aim goal of the plugin to work on Android and iOS?

I use it successfully on Android and iOS, when I debug on my mac in the browser I use a mockup.