Storage is not working without Android


#1

Hi everyone, I’m new to Ionic 2, and I’m having problems with Storage.

In the browser it works normally, but in Android (6.0), it does not work.

app.module.ts
import { Storage } from ‘@ionic/storage’;

@NgModule({
  declarations: [
    // ...
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // ...
  ],
  providers: [
      providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}, Storage]
  ]
})
export class AppModule {}

home.ts

import { Storage } from '@ionic/storage';

    storage.set('name', 'Leandro');

         storage.get('name').then((val) => {
           console.log("Name: " + val);
           alert("Name: " + val);
         })

I do not know where I went wrong

If anyone can help me please.

Thank you in advance.


#2

In the console log, can you check the storage provider the Android device is using?
You can provide the list of options, and order, in the app.module.ts, like:

..
providers: [
    ..
    {
        provide: Storage,
        useFactory: provideStorage
    }
    ..
..

export function provideStorage() {
    return new Storage(['sqlite', 'indexeddb', 'websql', 'localstorage'], {
        name        : '__mydb',
        size        : 4980736, // size of database, in bytes. WebSQL-only for now
        storeName   : 'keyvaluepairs', // should be alphanumeric, with underscores
        description : ''
    });
}

For sqlite you need a separate plugin, so I suggest you try ‘websql’ and ‘localstorage’ first.


#3

It appeared the following error:

Where am I going wrong? I put the code as you told me.

app.module.ts

  providers: [
    {
      provide: ErrorHandler, useClass: IonicErrorHandler
    },
    {
      provide: Storage,
      useFactory: provideStorage
    }
  ]

})
export class AppModule {}

export function provideStorage() {
    return new Storage(['websql', 'localstorage', 'indexeddb', 'sqlite'], {
        name        : '__mydb',
        size        : 4980736, // size of database, in bytes. WebSQL-only for now
        storeName   : 'keyvaluepairs', // should be alphanumeric, with underscores
        description : ''
    });
}

#4

My fault, I forgot to teel you to explicitly include the following import:

app.module.ts:

import {Storage} from "@ionic/storage";

#5

I added the line in

app.module.ts:

import {Storage} from "@ionic/storage";

But he continued the same way before. In the browser works, and when I install on android does not work