Using Storage in ionOnViewDidLoad() throws exception in RC0 / RC1


#1

Hi,
I’m starting to migrate some apps from beta11 to RC0/RC1 and stumbled across an exception while getting data from the Storage in the ionOnViewDidLoad event.

I’m able to reproduce this in the tutorial app created with ionic start testapp tutorial --v2. I added the Storage as Provider in app.module.ts and modified the hello-ionic.ts file:

import { Component } from '@angular/core';
import {Storage} from '@ionic/storage';


@Component({
  templateUrl: 'hello-ionic.html'
})
export class HelloIonicPage {
  constructor() {

  }

  ionViewDidLoad() {
    new Storage().get('testkey').then((value) => {
      console.log(value);
    });
  }
}

When I’m serving the app and navigate from the “Hello Ionic” page to “My First List” this works twice…the third time there is an exception:

error_handler.js:53 Error: Uncaught (in promise): TypeError: Cannot read property 'name' of null
    at s (polyfills.js:3)
    at s (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (ng_zone.js:227)
    at t.invokeTask (polyfills.js:3)
    at e.runTask (polyfills.js:3)
    at i (polyfills.js:3)
    at HTMLButtonElement.invoke (polyfills.js:3)

Seems like a bug or am I doing something wrong?

Thanks in advance and greetings from germany,

Veit


#2

Inject Storage into your constructor instead of creating a new instance every time.


#3

Thank you, sir…that worked! :slight_smile:

Is that documented somewhere? I tried to instantiate in constructor, but that doesn’t work.


#4

It’s standard practice in Angular applications; see Dependency Injection.


#5

And specifically for Ionic Storage that’s what the Usage example does.


#6

I know dependency injection, but i did not know, that Storage was annotated with @Injectable. Thanks for clarifying!