存储不返回字符串离子 [英] Storage not returning string ionic
问题描述
我已经实现了 Ionic的存储(不知道这是不是要存储的新东西)值).
I've implemented Storage for Ionic (don't know if it's something new to store values).
我已经创建了一个存储object
的服务(它可以工作,因为我已经用console.logged()了),但是当我想要get()
时,当我使用undefined
时它会返回我undefined
键,即使get()
方法中的console.log()
显示了我想要的...
I've created a service that stores an object
(it works, because I've console.logged() it), but when I want to get()
it's returning me undefined
when I use the same key, even when the console.log()
from the get()
method prints what I want...
getInfo(keystorage): string {
var val = null;
this.storage.get(keystorage).then((profile) => {
val = JSON.parse(profile);
console.log(val["info"]); //returning what I want
return val["info"];
})
.catch((err: any) => {
return 'catchhhh';
});
return val;
}
现在返回null
,因为我已经添加了var vall = null
,并且看起来它没有任何改变...
It's returning null
now because I've added var vall = null
and looks like it doesn't change anything...
我这样存储它:
saveInfo(usr){
if(usr==null) return;
var usertostore = {"id": currentUser["id"], "info":currentUser["info"]};
this.storage.set("userLoged",JSON.stringify(usertostore ))
}
我正在尝试获取如下信息:
I'm trying to get the info like this:
var userInfo = this.storageService.getInfo('myKey');
我想念什么?
推荐答案
之所以发生这种情况,是因为存储方法是异步的,并且在返回存储值时该值为null.
This happen because the storage method is asynchronous and the value is null when it is returned.
您可以尝试这种方式吗?
Can you try this way?
getInfo(keystorage) {
return this.storage.get(keystorage);
}
saveInfo(usr){
if(usr==null) return;
var usertostore = {"id": 1234, "info":"fdsgf"};
this.storage.set("userLoged",JSON.stringify(usertostore ))
}
并获取信息:
getInfo('userLoged').then((res) => {
var userInfo = res;
});
这篇关于存储不返回字符串离子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!