How to remove data from native storage in ionic 4

i’m actually trying to remove a precise data in my native storage and i try to use splice but i’m not sure that its working like this , do you have an idea ?

this.nativeStorage.getItem('AccepterPart').then(res => {
              for (let i of (JSON.parse(res))) {
                if (i['HISTO_N'] == this.currentNumInter) {
                  i['HISTO_Objet2'] = '106';
                  this.tabEffectuer.push(i);
                }
                var p = JSON.parse(res);
                var ind = p.findIndex(i => i['HISTO_N'] === this.currentNumInter);
                var inde = p.filter(i => i.HISTO_N !== this.currentNumInter);
                p.splice(inde);
                this.nativeStorage.setItem('AccepterPart', JSON.stringify(p));
              }
              this.nativeStorage.setItem('EffectuerPart', JSON.stringify(this.tabEffectuer));
            });  

“I’m not sure that it’s working” is doubly vague. Can you describe instead:

  • what behavior you are seeing;
  • what behavior you expected;
  • how exactly those two things are different?

i saw anything that’s the problem , i want when i click on a button too delete a precise data in the native storage from ionic 4 but at least it’s no working , the data i want to delete is already here.
I try to use nativeStorage.remove but its the same result

I would suggest using Ionic Storage instead, but what I am about to say applies to the NativeStorage you’re using at the moment as well.

Are you using storage to communicate amongst various parts of your app during the same app run? If so, the easiest and IMHO best thing you can do (which has a decent change of magically making the trouble you are having go away) is to follow this rule:

Read from storage only once per app run, during your app initialization

Once you’ve ensured that, then you can completely eliminate all race conditions from consideration by following this procedure:

  • put a console log to show what is being retrieved from storage at app start:
this.storage.ready()
.then(() => this.storage.get(KEY))
.then(gotten => {
  console.log("stored was: ", JSON.stringify(gotten));
});
  • launch the app, trigger whatever causes the modification code under test to fire
  • terminate the app
  • relaunch the app, connect Chrome Developer Tools to it
  • open JavaScript console
  • look in the console to see what was in storage
  • if it’s not what you expected, post what was there and what you expected (please, text, not images)
1 Like

Sorry for the response time but a took note about what you said but i used something new but i don’t know what it doesn’t work. I tried to use findIndex to know the position of my value in the key ‘AccepterPart’ and the splice to remove this value from my key but i don’t know why it doesn’t work ,Do you have an idea ?

this.nativeStorage.getItem('AccepterPart').then(res => {
              for (let i of (JSON.parse(res))) {
                if (i['HISTO_N'] == this.currentNumInter) {
                  i['HISTO_Objet2'] = '106';
                  this.tabEffectuer.push(i);
                }
                var p = JSON.parse(res);
                var ind = p.findIndex(i => i['HISTO_N'] === this.currentNumInter);
                var inde = p.filter(i => i.HISTO_N !== this.currentNumInter);
                p.splice(inde);
                this.nativeStorage.setItem('AccepterPart', JSON.stringify(p));
              }
              this.nativeStorage.setItem('EffectuerPart', JSON.stringify(this.tabEffectuer));
            });