Ionic Storage not working as expected


#1

i am fetching the stored data in ionic storage but i am getting null any help???


#2

Post some code, please, then we can help you.


#3

I’m just storing username and trying to fetch that in same function. It is returning null. username type is of ‘String’.


#4

storage.get and storage.set are asynchronous funcions. Problably storage.get has been executed before storage.set finish. Try it to confirm:

this.storage.set('username', this.username).then(() => {
  this.storage.get('username').then(data => {
    console.log(data)
  })
})

#5

While @jnascimento is absolutely correct in their analysis, I would urge you not to do this as a matter of principle, because it means you have to worry religiously about race conditions in all your code that interacts with storage.

Instead, use storage only to communicate with future invocations of the app, writing optimistically and reading only once, at app startup. For all in-app communication, eschew storage in favor of data held in a service provider and shared (preferably exposed as Observables).


#6

thank you
it is working