Ionic 4 storage values going null on android device

I am using cordova-sqlite-plugin and @ionic/storage to locally store the data on the device. On browser I am easily getting stored values, but on the android device all values are going null. Here is my code :

this.storage.get('name').then((val) =>{
      console.log("name is", val)
      this.name = val;
      console.log(this.name)


 this.storage.get('role').then((val) =>{
        console.log("role" , val)
        this.role = val;
        console.log(this.role)

      })

On the android device, both values are going null whereas working fine on the browser. Thanks in advance

Hi
the code does not show setters, so difficult to say.
Tom

       this.storage.set('role',"Resellers")

     submit(email, name ){
        
        
        let mail = email
        let nm = name
    
        this.storage.set('name',nm)
    }

the role is set with string and name is set with values passed with submit() function which is called on the button click

I am getting both values on browser but not on device

You are using this Plugin right?

Then your code seems to be right, and as you said it is working on the browser.
So in order to bugfix this we would need more code. What are you doing with the variables after you got them.
Maybe your Code is going forward to fast while android is still loading the storage item?
This happened to me once. Because it takes a while to get the Item from the Device Storage you sometimes must wait for the item and then go on with your code.

Yes I am using same plugin

constructor(private storage: Storage){

this.storage.get('name').then((val) =>{
      console.log("name is", val)
      this.name = val;
      console.log(this.name)

    })

      this.storage.get('role').then((val) =>{
        console.log("role" , val)
        this.role = val;
        console.log(this.role)
  
      })

}

I want to navigate user depending upon values stored, but for the time being I have only this much code in my ts file

Can you please run this on your device and see if values are here:

this.storage.get('name').then((val) =>{
      alert(val);
      this.name = val;
    })

      this.storage.get('role').then((val) =>{
        alert(val);
        this.role = val;
      })

By the way which device are you using?

Okay I will try. I am using realme 3 pro andorid 9

Does this work and display a string? If so, then you have a an async issue where you need to wait for the setter to finish before to get

this.storage.set('stuff','My stuff')
       .then(()=> this.storage.get('stuff'))
       .then((stuff)=>console.log(stuff));

$5 on "forgot to wait on Storage.ready()".

2 Likes

Sorry for the late reply. You are right, this stuff is working properly but now how do I wait?

 this.storage.ready().then(() =>{
      console.log('before')
     return this.storage.get('name')
    
    }).then((name) => console.log('after' + name) )

I have tried something like this which is also returning null

async getName() {
    await this.storage.ready();
    console.log('ready');
    const name = await this.storage.get('name');
    console.log('retrieved', name);
}

No . Still returning null on android

How are you setting name in storage? Perhaps it’s not being set properly (or storage.ready() again) if you are still getting null

yes. I have set properly. I am getting proper values with ionic serve

The code you posted for the submit method does not include storage.ready() before storage.set()

The problem is I am setting values on another page and getting values on another. Is that the reason for await issue?

And this is why I keep insisting that people should not use storage for in-app communication. If you only use storage to communicate between this app run and the next, you never have to worry about any of this. Use a mutually injected service provider instead to communicate amongst different parts of the app.

But I want to redirect user depending upon values set. I value is set which means old user redirect to some page , if value is not set means new user redirect to details page. How can I achive this ?

Read from storage only once, at application startup, and then perform your routing accordingly.