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
Hi@RobertLinde
You can setvalue to the variable
public baum='max';
setStorage(){
this.storage.set('Nickname',this.baux);
}
2 Likes
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.
1 Like
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?
2 Likes
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
1 Like
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”.
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
1 Like
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.
baum:string="Nickname'
setStorage(){
this.storage.set(this.baum, 'Max');
}
getStorage(){
this.storage.get(this.baum).then((val) => {
console.log('StorageNickname: ', val);
});
}
1 Like
yeah and it had the value it should have
This isn´t working either
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
1 Like
This is the appropriate doc
1 Like
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)})})
}
1 Like
yesroh
June 7, 2018, 9:09pm
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
1 Like
Sorry for my late answer guys, I got seriously tilted ´cause nothing worked.
I finally found a solution, if somebody needs it just hit me here.
dimsal
February 25, 2019, 1:50pm
20
@RobertLinde Can you share your solution thanks?