Ionic2- unable to remove value from storage


#1

I’m making a simple hybrid app using which users can search for word meaning and bookmark words if they want and remove the bookmark as well.
I use Ionic2 storage for saving the words. I’m able to save value (bookmark words) but when I try to remove the bookmark, the process doesn’t happen.
here’s a screen shot of the stored data.

I am trying to remove a word from favorites array. But it ain’t happening.

home.ts

import { BabyRhymes } from './../../app/shared/babyRhymes.service';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

@Component({
  selector: 'page-word-definition',
  templateUrl: 'home.html',
})
export class Home {
  word:string;
  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              private babyrhymes: BabyRhymes) {

  }

  removeThisWord(word)
  {
    this.babyrhymes.unfavoriteWord(word);
  }
}

From home.ts, I call the service which removes the word. Code for my service is as below:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import 'rxjs/Rx';
import { Storage } from '@ionic/storage';

@Injectable()
export class BabyRhymes{
    constructor(private storage: Storage){
    }

    unfavoriteWord(word)
    {
      this.storage.ready().then(
      () => {
        this.storage.remove(word);
      });
    }
}

I am new to ionic2. Can anybody point out the mistake I made?
Also, a side-question: By storing these values to Storage, am I really making use of SQLite or is it just indexedDb or WebSQL? Sometimes these values gets stored in IndexedDB and most of the time its in WEB SQL.
In app.module.ts, under imports, I gave the following:

IonicStorageModule.forRoot({
  name: '__babydb',
  driverOrder: ['indexeddb', 'sqlite', 'websql']
})

Thanks!


#2

You can’t remove just a value in your storage. You can remove just keys like your favorites or history keys.

From the Ionic Storage Docs:

remove(key)
Remove any value associated with this key.

If you want to remove a word in your array you have to remove it with array.splice(index,1) and then you can store this array again in your storage with storage.set('key',data)


#3

I quote the docs at https://ionicframework.com/docs/storage/:

When running in a native app context, Storage will prioritize using SQLite, as it’s one of the most stable and widely used file-based databases, and avoids some of the pitfalls of things like localstorage and IndexedDB, such as the OS deciding to clear out such data in low disk-space situations.

When running in the web or as a Progressive Web App, Storage will attempt to use IndexedDB, WebSQL, and localstorage, in that order.

(emphasis mine)


#4

what if i want to check if the key exist or no before i try to remove it,
because i tried to remove the storage from the browse manually and then try to log out , the logout function try to remove the token from storage but didn’t find it : i have the following error:
Uncaught (in promise): InvalidStateError: Failed to execute ‘transaction’ on ‘IDBDatabase’: The database connection is closing.


#5

I had a similar problem in iOS and it seems it’s solved by removing the:

driverOrder: ['indexeddb', 'sqlite', 'websql']

I started to get this problem after the last updates of iOS, so it’s probably related to the preference of IndexedDB against the others.


#6

Work for me lucapale, very weird