Save Storage Value as variable


#1

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

baum:any;
setStorage(){
		this.storage.set('Nickname',  'Max');
	}
	

	getStorage(){
		this.storage.get('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?

Greetings

Robert


#2

Hi@RobertLinde
You can setvalue to the variable

public baum='max';
setStorage(){
		this.storage.set('Nickname',this.baux);
}

#3

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.


#4

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(){
		this.storage.get('Nickname').then((val) => {
                     this.new=val;
			console.log('StorageNickname: ',this.new);
		});
	}

You wanna something like this?


#5

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


#6

Hi @RobertLinde
Try the above code


#7

Thanks for your answer. With your code I can access the variable only inside this area:

this.storage.get('Nickname').then((val) => {
                     this.new=val;
			console.log('StorageNickname: ',this.new);
		});

If I want to access it like this:

this.storage.get('Nickname').then((val) => {
                     this.new=val;
		});
console.log('StorageNickname: ',this.new);

it says “undefined”.


#8
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


#9

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.


#10

Did u console the val ?


#11
baum:string="Nickname'
setStorage(){
		this.storage.set(this.baum,  'Max');
	}
	

	getStorage(){
		this.storage.get(this.baum).then((val) => {
			console.log('StorageNickname: ', val);
		});
	}

#12

yeah and it had the value it should have


#13

This isn´t working either :confused:


#14

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


#15

This is the appropriate doc


#16
baum:string="Nickname';

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

or uglier:


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

#17

You can use async await like this:

async getLocalStorageValue(): Promise<IValue> {
    return await this.storage.get('key');
}

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


#18

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.


#19

share your solution!