I’m trying to update an array in Ionic 4 with the following Data Structure,
[{"name":"foo","country":"boo","photos":['img1.jpeg', 'img2.jpeg']}]
My storage.service.ts is like,
import { Injectable } from '@angular/core';
import { Plugins, Capacitor } from '@capacitor/core';
const { Storage } = Plugins;
@Injectable({
providedIn: 'root'
})
export class StorageService {
public objects = [];
constructor() { }
async setObject(key: string, value: any) {
let retrieve_key = await Storage.get({ key });
if (retrieve_key.value==null){
await Storage.set({key, value: JSON.stringify([value])});
}
else{
this.objects = (JSON.parse(retrieve_key.value));
this.objects.length>0?this.clear:console.log('Pushed')
await Storage.set({ key, value: JSON.stringify(this.objects.unshift(value))});
}
}
async clear() {
await Storage.clear();
}
}
My person.page.ts is like,
setStorage() {
//this.storage.setString('name', this.name);
this.storage.setObject('person', {
name: this.name,
country: this.country,
photosx: this.photoService.photos
});
}
I am able to create the array but unable to update the array with new value, stuck with following error,
ERROR Error: Uncaught (in promise): TypeError: this.objects.unshift is not a function
TypeError: this.objects.unshift is not a function
at StorageService.<anonymous> (storage.service.ts:32)
at Generator.next (<anonymous>)
at fulfilled (tslib.es6.js:71)
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (core.js:41667)
at ZoneDelegate.invoke (zone-evergreen.js:363)
at Zone.run (zone-evergreen.js:123)
at zone-evergreen.js:857
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:41645)
at resolvePromise (zone-evergreen.js:798)
at zone-evergreen.js:705
at fulfilled (tslib.es6.js:71)
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (core.js:41667)
at ZoneDelegate.invoke (zone-evergreen.js:363)
at Zone.run (zone-evergreen.js:123)
at zone-evergreen.js:857
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:41645)
What’s wrong I am doing?