Save Storage Value as variable


I want to save a storage value as variable.
This is my code:

setStorage(){'Nickname',  'Max');

	getStorage(){'Nickname').then((val) => {
			console.log('StorageNickname: ', val);

baum should be the variable, so in the end baum should have the value “Max”.
Is there a way to do this?




You can setvalue to the variable

public baum='max';


Thanks for you answer.
The problem is that I need to do it the other way around. Means I want to get the value from the storage and use it as value for my variable and not set the storage.


Sorry I cant get you.Actually you want to get the value and set the value in to a variable?
Am i right @RobertLinde

public new;
getStorage(){'Nickname').then((val) => {
			console.log('StorageNickname: ',;

You wanna something like this?


yeah correct. I want to get the value from the storage and set the value in to my variable.


Hi @RobertLinde
Try the above code


Thanks for your answer. With your code I can access the variable only inside this area:'Nickname').then((val) => {
			console.log('StorageNickname: ',;

If I want to access it like this:'Nickname').then((val) => {
console.log('StorageNickname: ',;

it says “undefined”.

If the getvalue only gives value if you set the value.
So you have to set the value or  call the set value fuction first.
Now your setvalue is not working properly thats why you getting undefined.

Please Refer the documentation ionic Native storage


Yeah it is set. I can also show the value of the storage in the console, so it´s obviously set. But I can´t get this value into a variable.


Did u console the val ?

setStorage(){,  'Max');

	getStorage(){ => {
			console.log('StorageNickname: ', val);


yeah and it had the value it should have


This isn´t working either :confused:


It should

Unless you call both methods consecutively

They are async so should wait for each other

Which, btw, in your case requires a timeout or user action


This is the appropriate doc


doStuff() {
    console.log('What is my nick',this.baum);,  'Max')
        .then((val) => {console.log('StorageNickname: ', val)})

or uglier:

doStuff() {
    console.log('What is my nick',this.baum);,  'Max')
                .then((val) => {console.log('StorageNickname: ', val)})})


You can use async await like this:

async getLocalStorageValue(): Promise<IValue> {
    return await'key');

using async the value only gets returned when the promise inside is resolved


Sorry for my late answer guys, I got seriously tilted ´cause nothing worked. :sweat_smile:

I finally found a solution, if somebody needs it just hit me here.


share your solution!


@RobertLinde Can you share your solution thanks?