[英]Wait for a subscribe method in Ionic
我在提供程序中有一種方法
getProfile() {
let headers = new Headers();
this.loadToken();
headers.append('Authorization', this.authToken);
headers.append('Content-Type','application/json');
return this.http.get(this.db_url + 'profile',{headers: headers})
.map(res => res.json());
}
我在另一個提供商中調用上述方法
getProfile() {
this.authService.getProfile().subscribe((profile: any) => {
this.profile = profile.user._id;
},(err : any) => console.log(err))
}
在這里我要停止執行代碼,直到執行this.profile = profile.user._id
這行。在聲明getProfile()
方法的同一provider
中,我將http請求作為
let headers = new Headers();
headers.append('Content-Type','application/json');
let selected = {
address: home.address,
date: new Date(),
id: this.profile // from the getProfile() file.
}
console.log(selected);
id
的輸出是undefined
。 我認為這里存在異步。
將此代碼放在類似的函數中
setHeadres(){
let headers = new Headers();
headers.append('Content-Type','application/json');
let selected = {
address: home.address,
date: new Date(),
id: this.profile // from the getProfile() file.
}
}
並在getProfile()
函數內調用此函數,如下所示
getProfile() {
this.authService.getProfile().subscribe((profile: any) => {
this.profile = profile.user._id;
this.setHeaders();
},(err : any) => console.log(err))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.