Search code examples
ionic-frameworkstorageionic-storagecordova-sqlite-storage

set storage of an object of arrays in Ionic 5


I have an ionic 5 app to be build for browser. I have installed @ionic/Storage, etc. I have a function in my service the current iteration is as follows:

async setStorageObj( key: string, values: any) {
console.log(key, values);
return this.storageCtrl.set( key, values );

}

"key" is a single string "product attributes" and values is an object of arrays(I think). It looks like this in the developer console: Product Attributes: [sportsdemo_x_xxx: [{key:value pairs}, {key:value pairs}, {key:value pairs}], sportsdemo_y_yyyyy: [{key:value pairs}, {key:value pairs}], ...etc]

The console log above shows correct in the console so I know the values are there. However the its showing up in storage as "ProductAttributes: []. I am simply unable to figure out the correct syntax. I've been at it for a awhile and its late... maybe I am just tired. Your help would be appreciated.


Solution

  • I came up with two solution to this question:

    1. loop through the object of arrays and set each array

      for (k in object) { 
         this.storageCtrl.set (k, object(k)); }
      

    or

    1. the solution I went with:

      async setStorageObj( values: any[]) {
          return await this.storageCtrl.set("productAttributes", JSON.stringify(values));
      }