How to get All the Data Inside the NativeStorage


#1

here’s my code.
i can get a selected key’s item, but i cant get all the item in this function getAllItemLocal() .

export class HomePage {
  i:any;
  dl = 0;

  private native = {
    key: '',
    value: ''
  }
  private selected: any;
  private gettingItem: any;

  constructor(public navCtrl: NavController, private toast: ToastController, private nativeStorage: NativeStorage) {
    this.selected = '';
    console.log( this.native.key.length);
  }

  //Toast Message//
  showToast(msg) {
    let toast = this.toast.create({
      message: msg,
      duration: 3000,
      position: 'buttom'

    })
    toast.present();
  }

//Saved function//
  saved(native) {
    this.nativeStorage.setItem( this.native.key, this.native.value).then((d) => {
      console.log('storage save', d);
      this.dl = this.dl + 1;
      console.log(this.dl);
      this.showToast('successfuly saved to native Storage !');

    }, (e) => {
      console.log('unable to save', e);
      this.showToast('unable to saved to native storage');
    })
  }




  // Getting Selected key/ values from native storage
  getSelectedItemLocal(i){
 this.nativeStorage.getItem(i).then((d)=>{
    console.log(d);
    this.showToast('successfuly get selected');
    // this.gettingItem = d;
  },(e)=>{
    console.log('getting err',e);
    this.showToast('unable to get local from native storage')
  })
  }

  // Getting All Key /values from native storage
  getAllItemLocal() {
    for (this.i = 1; this.i <= this.dl; this.i++) {
    this.nativeStorage.getItem(this.i).then((d) => {
      console.log(d);
      this.showToast('successfuly get selected', );
      // this.gettingItem = d;
    }, (e) => {
      console.log('getting err', e);
      this.showToast('unable to get local from native storage')
    })
  }
}

#3

Is there a reason you’re using NativeStorage instead of ordinary Storage? Storage has a keys() method that allows easy retrieval of everything.


#4

yeah i do have thing like key(). But its seemed like its only take the array, but not the value inside. Could u be teach me how to do that so that i can get the value of the key.

  // Getting key from native storage
  getAllLocal(){
   this.nativeStorage.keys().then((d)=>{
    console.log(d);
  },(e)=>{
    console.log('getting err',e);
    this.showToast('unable to get local from native storage')
  })
  }

#5

You didn’t answer my question about why you’re not just using ordinary Storage, but I suppose the principle is the same:

storage.keys().then((keys) => {
  return Promise.all(keys.map(k => storage.get(k)));
});

#6

Sorry for the late reply . there’s no special thing i want to use the native storage compare to the local storage…hmm because back then i don’t know there’s only way to store data which is to use native storage.


#7

Read this then.