Search code examples
angulartypescriptcordova-pluginsionic3cordova-nativestorage

ionic 3 native storage read value stored


Hello,

in my code I tried the code written on the page of the Native Storage plugin which is here : Native Storage

    import { NativeStorage } from '@ionic-native/native-storage';

constructor(private nativeStorage: NativeStorage) { }

...

this.nativeStorage.setItem('myitem', {property: 'value', anotherProperty: 'anotherValue'})
  .then(
    () => console.log('Stored item!'),
    error => console.error('Error storing item', error)
  );

this.nativeStorage.getItem('myitem')
  .then(
    data => console.log(data),
    error => console.error(error)
  );

When I launch my android simulator device the console sends me back this :

[00:02:01]  console.log: Stored item! 
[00:02:01]  console.log: [object Object] 

console

I would like to find a solution to read the stored information. Because I would like to use a value stored in native storage to make a condition on an external page and I can not. Example "If the value stored on native storage of the name Vibrator is == to true then we start this function". I am looking for the method to read the value. Can you help me?

Thank you


Solution

  • To read the value, call it like this:

    console.log(data.property);
    console.log(data.anotherProperty);
    

    Refer to the plugin github to get a clearer understanding: https://github.com/TheCocoaProject/cordova-plugin-nativestorage