Ionic Storage returning null


#1

My ionic storage returns null data to page though it actually has data, when I log in provider inside the function I am getting the correct values, but when I call it via page and use then attacher to get value, I am getting null value.

Function in provider:

  async getSavedArticles() {
    this.storage.ready().then(() => {
      return this.storage.get('savedArticles');
    });
  }

In my Page:

    this.storage.getSavedArticles().then((value) => {
      console.log(value);
      this.savedArticles = value;
    });

Please help, thanks.


#2

Make sure you’re waiting on storage.ready() before trying to interact with it. I also am a huge unfan of async, for whatever that’s worth. I think it makes code much less readable.


#3

I don’t work with async but if you add ‘return’ before this.storage.ready() ?

async getSavedArticles() {
    return this.storage.ready().then(() => {
      return this.storage.get('savedArticles');
    });
  }

#4

It won’t work as I want the promise for get method not the ready one.


#5

Yes, but returning the ready promise will result in it returning your get promise.