我想在ionic3未定義響應在ionic3
submitLoginForm() {
var username = this.formData.value.username;
var password = this.formData.value.password;
var bodyjson;
var userId;
this.ApidataService.logincheck(username,password).subscribe(data => {
this.logindetail = data;
bodyjson = JSON.parse(this.logindetail._body);
userId = bodyjson.user.id;
if(userId != undefined){
this.ApidataService.userdata(userId).subscribe(data => {
this.userInfo = data;
});
}
console.log(userId);
});
console.log(this.userInfo);
}
實現嵌套調用API現在,當我調用該函數未定義第一個呼叫,然後從第二個呼叫起嵌套API返回它會返回適當的值,但是當我嘗試記錄正在發送給api的參數時,我注意到每次都傳遞了正確的值。我相信這是由於一些同步問題,但無法找出問題和解決方案。
,而不是'(!用戶id =未定義)'只是檢查像'(用戶ID)' –
你可以找到的角版本重複在這裏:https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2。你正在做的是試圖同步訪問異步操作的響應。 Javascript不能像那樣工作。該鏈接涵蓋了js的異步行爲的fundemantals。如果你想連鎖你的observable,你可以檢查:https://stackoverflow.com/questions/36712659/angular-2-two-backend-service-calls-on-success-of-first-service/36712707#36712707 – echonax