How to remove a SQLite storage item which is populated using forEach()


#1

Hello guys,

I’m using Ionic storage. I have two pages. The first page is for saving items to favorite list.
The second page is a list of favorite items which has delete function.
I added several items using this command on the first page.

this.storage.set(item ${ this.item.id }, this.item;

The first page is linked to dataProvider. This saves item with key: item {{ item’s id number ]} to SQlite storage along with all of its attributes from dataProvider.

On the second page, I’m fetching all saved items from storage to a list using below command:

  ionViewDidLoad() {
    this.storage.forEach( (item) => {
      this.storageItems.push(item);
     })
}

and this can fetch all items from storage with *ngFor directive and {{ item.name }}, {{ item.description }}

I’m trying to delete the item using: this.storage.remove(item ${ this.item.id });
However it throws me an error:
Cannot read property 'id' of undefined

How can I work around this? this delete command works well on the first page which has imported dataProvider.
The second page is fetching items using interface and storage.

export interface StorageItem {
    name: string,
    description: string,
   id: nubmer;
}

I’m using *ngFor="let item of StorageItems" on second page.

this.storageItems.splice(index, 1);
didn’t work… I’ve tried this.storageItems.pop() as well. None of them can erase an item from favorite list although items can be erased from the first page which received item’s attributes directly from the dataProvider.

What am I missing here?
Any help would be appreciated!

Thanks in advance


#2

still no solution for this… any advice would be appreciated.


#3

Can you just set each item by Id?

storage.set(`${item.id}`, item)

And fetch by item.id?

The way your code looks, it appears you’re using a combo of the item itself + its Id where it should just be the item Id as a string.

So

storage.remove(`${item.id}`)

Could / should work


#4

Thanks for your response.

I’m still getting the same error message: “Cannot read property “id” of undefined”
It looks like the page which is fetching items from the storage is not able to pass in id to function… any other opinion would be appreciated.