How to get stored data in Ionic 2 Storage using synchronize mehod


#1

Hello Ionic 2 programmers.
I used Ionic 2 Storage to save data.

this.storage.set('username', 'value1');
this.storage.set('usertype', 'value2');

How to get data as synchronize?

I have created Class for set and get data.

export class UserData {
username: string;
constructor(public storage: Storage) {}
setUsername(username: string) {
   this.storage.set('username', username);
};
getUsername() {
  return this.storage.get('username').then((value) => {
    this.username = value;
    return value;
  });
};

But I have never anything.


#2

You don’t. You need to refactor your consumer code to deal with that fact.


#3

Thank you @rapropos.
How to refactor my code?
Can you help me?


#4

The only time you can rely on the data being available is in a then block. If that’s problematic, initialize wherever the data is being accessed (typically from within a template) to a dummy value.


#5

Do you have any sample link?
I am a beginner with Ionic 2.