Javascript 承诺-从存储器获取参数
我是新加入Promissions的,我发现我必须使用Promissions从本地存储中获取保存的信息。我猜post请求在我设置前被调用Javascript 承诺-从存储器获取参数,javascript,angular,promise,ionic2,Javascript,Angular,Promise,Ionic2,我是新加入Promissions的,我发现我必须使用Promissions从本地存储中获取保存的信息。我猜post请求在我设置前被调用 参数集('az_键',数据)。我怎样才能解决这个问题?尝试了承诺内的返回语句,但这会导致错误 public call(params : URLSearchParams) { this.storage.getAZKey().then(data=>{ console.log(data); params.set('az_key', data
参数集('az_键',数据)
。我怎样才能解决这个问题?尝试了承诺内的返回语句,但这会导致错误
public call(params : URLSearchParams) {
this.storage.getAZKey().then(data=>{
console.log(data);
params.set('az_key', data);
console.log("AZ-Key: " + params.get("az_key"));
})
console.log("Token for XXrequest: " + params.get("az_key")); // gives me null
return this.http
.post("http://localhost:8080/Info", params)
.map(res => res.text())
}
}
您不需要承诺从本地存储中获取某些内容 设置某事:
localStorage.setItem("username", "John");
localStorage.getItem("username");
为了得到一些东西:
localStorage.setItem("username", "John");
localStorage.getItem("username");
注意:您只能设置和获取字符串
参考:
但是对于异步问题;您是对的,您的post请求“可能”在设置参数之前被调用,因为这些是异步函数
一种方法是链接这些异步函数,如:
public call(params : URLSearchParams) {
return this.storage.getAZKey().then(data=>{
console.log(data);
params.set('az_key', data);
console.log("AZ-Key: " + params.get("az_key"));
}).then(data=>{
console.log("Token for XXrequest: " + params.get("az_key")); // gives me null
return this.http
.post("http://localhost:8080/Info", params)
.map(res => res.text())
})
}
当您使用此方法时:
this.servletService.call(params).then((obs)=>{
obs.subscribe(
(data)=>{
console.log(data);
},
(err)=>{console.log(err);})
});
但我用的是@IONAL/storage@SaskiaB我更新了关于异步问题的答案。但是,如果您在回调中遇到错误,这可能也不起作用。您遇到了什么样的错误?我在调用方法上有一个订户:
this.servletService.call(params).subscribe(…)
,当我像上面那样执行时,它会说属性“subscribe”在类型“void”上不存在。
@SaskiaB哦,很抱歉,我忘记了承诺的返回。我用承诺的返回声明更新了我的答案,但仍然无法使用订阅。