Bookmark with NativeStorage

Hi, I’m trying to save fav on account/ page and to display from different pages.

In dynamic page read.html I have this:

public itemBookmark = [];
	bookmarkSave()
	{
		this.itemBookmark.push(this.url);
		this.nativeStorage.setItem('bookmark',
		{
		  itemBookmarked: this.itemBookmark
		})
		.then(
		  () => {
			alert("Saved on bookmark");
		  },
		  error =>
		  {
			console.log('Error storing item')
		  }
		);
	}

and in account.html

  bookmark: any;
  ionViewWillEnter()
  {
    this.nativeStorage.getItem('bookmark')
    .then(
      data =>
      {
        this.bookmark = {itemBookmarked: data.itemBookmarked};
        alert(data.itemBookmarked)
      },
      error =>
      {
       //
      }
    );
  }

the problem is that it works only with 1 bookmark. Every time I try to bookmark other item it replaces the existing one.
I also tried to inside account.html to getItem(‘bookmark’) and it it exists to push another item but It didn’t work.
Do you have any ideas how to save more items inside 1 storage?
Thank you!

@nogstudio

I have the same problem
can you help me

@demir50
Hi, i think you are not understanding how the storage works.

In storage you save data providing throught setItem method two things:

A key and a value associate to the key.

So in your code, you are storing ONE object associate with ‘bookmark’ key. By this way when you save a new bookmark with the key ‘bookmark’ the setItem method looks for your key if it does not exist it creates a a key-value but IF EXIST it replaces with the new value.

You should store an array associate with the key and retrive that array to save or delete bookmarks and the save in the storage the edited array.

Hope its understable!

1 Like