How to use @ionic/storage in providers

#1

I have referred this doc https://ionicframework.com/docs/storage/ but when i am importing storage in providers(@Injectable) i am not able to set and get the data…

is there any solutions??

my ionic info is

ordova CLI: 6.5.0
Ionic Framework Version: 2.0.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.9.5
Xcode version: Not installed

#2

Probably. Nobody is going to be able to say anything more concrete than that with such a vague question and no code.

1 Like
#3

If you’ve little older version of Ionic installed, then you can try this Ionic Storage: Ionic 2 orelse wait for 30minutes and I’ll post new video about the latest way of using local storage in Ionic 2.

#4

In app.module.ts

import { IonicStorageModule } from ‘@ionic/storage’;

@NgModule({
declarations: [
MyApp,
StarterPage,
HomePage,
InvitePage
],
imports: [
IonicModule.forRoot(MyApp, {
modalEnter: ‘modal-slide-in’,
modalLeave: ‘modal-slide-out’,
pageTransition: ‘ios-transition’
}),
IonicStorageModule.forRoot(),
CloudModule.forRoot(cloudSettings)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
StarterPage,
HomePage,
InvitePage
],
providers: [
DataService,
{ provide: ErrorHandler, useClass: IonicErrorHandler }
]


in providers/data-service.ts

import { Storage } from ‘@ionic/storage’;

constructor(

public storage: Storage,
public alertCtrl: AlertController,
public toastCtrl: ToastController

) {
// console.log(‘Hello DataService Provider’);

storage.ready().then(() => {
  
});

}

setValue(key: string, value: any) {
this.storage.set(key, value);
return true;

}
getValue(key: string) {

this.storage.get(key).then((val) => {
    return val;
  });

}


when i am trying to get it returns null

#5

print the string present inside key variable(both in setValue and getValue methods) and see …sometimes these simple things cause a lot of trouble!

#6

Nope, Same result it returns null… i am stuck in this place, i going through many blogs and posts but unfortunately none have been worked :confused:

#7

Instead of passing the key as parameter, type it yourself in set and get method and check the returned value.

#8

yup this working fine in component but not in providers

#9

Did you check Ionic Storage: Ionic 2 tutorial ?
Inside above Tutorial: Check the code inside “Data Provider” where there is set and get method.

#10

I don’t know what “it” you’re talking about, but getValue() as it’s written doesn’t return anything. If you’re expecting it to synchronously return the val that is coming out of storage, it’s not going to, no matter what you do. This has nothing to do with being in a provider or a component; it’s a fundamental fact of how asynchronous programming works. You should make it return the promise you are getting from storage, and anybody who calls it is responsible for chaining a then() clause off of it:

getValue(key: string): Promise<any> {
  return this.storage.get(key);
}
3 Likes
#11

Thank you… Promise works fine

#12

Great but hou do i get the value in another provider
i used console.log(‘token out setDp:’, this.authProvider.getValue(‘authToken’));
it is returning null