# IONIC 4 - Cordova file plugin

**URL:** <https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138>\
**Category:** Ionic Native\
**Created:** [February 21, 2019, 11:37pm UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138 "2019-02-21T23:37:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratedam](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@ratedam](https://forum.ionicframework.com/u/ratedam)\
**Post date:** [February 21, 2019, 11:37pm UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/1 "2019-02-21T23:37:01Z")

</div>

Hello!  
I’m currently trying an app on ionic 4 and i need to use the file plugin ([https://ionicframework.com/docs/native/file/](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:

```auto
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

```auto
basemodelservice.save();

```

And i get the error

```auto
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’;

```auto
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});
  }
}

```

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [February 22, 2019, 12:00am UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/2 "2019-02-22T00:00:27Z")

</div>

Hi @ratedam,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![ratedam](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@ratedam](https://forum.ionicframework.com/u/ratedam)\
**Post date:** [February 22, 2019, 12:30am UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/3 "2019-02-22T00:30:54Z")

</div>

> [@FdezRomero](#):
>
> import { Platform } from ‘@ionic/angular’;

Hello Rodrigo,

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

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

```

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [February 22, 2019, 12:40am UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/4 "2019-02-22T00:40:24Z")

</div>

> [@FdezRomero](#):
>
> await this.platform.ready();

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

---

<div class="post-metadata">

**Author:** ![ratedam](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@ratedam](https://forum.ionicframework.com/u/ratedam)\
**Post date:** [February 22, 2019, 9:40am UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/5 "2019-02-22T09:40:10Z")

</div>

Hello!

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

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [February 22, 2019, 10:15am UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/6 "2019-02-22T10:15:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratedam](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@ratedam](https://forum.ionicframework.com/u/ratedam)\
**Post date:** [February 23, 2019, 1:35am UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/7 "2019-02-23T01:35:23Z")

</div>

Hello,

Here’s my code

```auto
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();`

---

<div class="post-metadata">

**Author:** ![ratedam](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@ratedam](https://forum.ionicframework.com/u/ratedam)\
**Post date:** [February 23, 2019, 3:34pm UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/8 "2019-02-23T15:34:06Z")

</div>

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

Thank you!

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [February 23, 2019, 10:47pm UTC](https://forum.ionicframework.com/t/ionic-4-cordova-file-plugin/157138/9 "2019-02-23T22:47:13Z")

</div>

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