IONIC 4 - Cordova file plugin

Hello!
I’m currently trying an app on ionic 4 and i need to use the file plugin (https://ionicframework.com/docs/native/file/).

Here’s what i made:

  • Installed the required packages with ionic cordova plugin add cordova-plugin-file && npm install @ionic-native/file. Here’s the class below:
import { Injectable } from '@angular/core';
import { File } from '@ionic-native/file';

@Injectable({
  providedIn: 'root'
})
export class BaseModelService {
  constructor(private file: File) { }

  save(){
      return this.file.writeFile(this.file.dataDirectory, 'hello.json', JSON.stringify({test:'value'}), {replace:true})

  }
}

In my page class i after some logic i simply call

basemodelservice.save();

And i get the error

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'writeFile' of undefined

What am i doing wrong? Thank you!

Edit: I was able to solve my issue. Here’s how:

I had to change the import from import { File } from ‘@ionic-native/file’; to import { File } from ‘@ionic-native/file/ngx’;

import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';
import { File } from '@ionic-native/file/ngx';

@Injectable({
  providedIn: 'root'
})
export class BaseModelService {
  constructor(private file: File, private platform: Platform) {}

  async save() {
    await this.platform.ready();
    return this.file.writeFile(this.file.dataDirectory, 'hello.json', JSON.stringify({test:'value'}), {replace:true});
  }
}

Hi @ratedam,

You have to wait for the plugins to be ready before you can use them, by using platform.ready():

import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';
import { File } from '@ionic-native/file';

@Injectable({
  providedIn: 'root'
})
export class BaseModelService {
  constructor(private file: File, private platform: Platform) {}

  async save() {
    await this.platform.ready();
    return this.file.writeFile(this.file.dataDirectory, 'hello.json', JSON.stringify({test:'value'}), {replace:true});
  }
}

Best,
Rodrigo

Hello Rodrigo,

Thank you for your amazing explanation however after impleting and adapting the code mentioned above i’m getting the following error:

Error: Uncaught (in promise): TypeError: Cannot read property 'ready' of undefined

Sorry, I wrote it from memory and missed a this. before. It should be await this.platform.ready();. I already corrected the example above.

Hello!

I noticed that you had forgotten the “this” and added it on my test code, so the problem remains even with the this.

Have you added import { Platform } from '@ionic/angular'; at the top and private platform: Platform in the constructor?

Hello,

Here’s my code

import { Injectable } from '@angular/core';
import { File } from '@ionic-native/file';
import { Platform } from '@ionic/angular';


@Injectable({
  providedIn: 'root'
})
export class BaseModelService {


  public data: any = [];

  constructor(private file: File, private platform: Platform) { }

  async save() {      
    await this.platform.ready();
    return this.file.writeFile(this.file.dataDirectory, 'hello.json', JSON.stringify({test:'value'}), {replace:true});
  }

}

Right now i’m getting the error:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'ready' of undefined

on this line: await this.platform.ready();

I was able to solve my issue. The solution is in the top!

Thank you!

1 Like

I’m glad you found the other problem. Cheers!