0
我有以下功能是服務提供者。我想分開保留後端和UI部分,因此我使用提供程序來提取所有數據。Apollo Angular2客戶端處理承諾
getUserById(id: number) {
return new Promise((resolve, reject) => {
this.apollo.query({
query: UserByIdQueryText,
variables: {
id: id
}
}).subscribe((res: any) => {
let ans = res.data.user;
if (ans) {
console.log(ans);
resolve(ans);
} else {
reject('could not get user');
}
}, (err) => {
console.error(err);
});
});
}
在實際的頁面,我有下面的代碼來獲取數據。
export class UserProfilePage {
public user: User;
public id: number;
constructor(private userService: UserService, private navController: NavController, private params: NavParams) {
this.user = null;
this.id = params.get("id");
this.userService.getUserById(this.id)
.then((user: User) => {
this.user = user;
});
}
}
問題是遠程調用遲到,但視圖試圖顯示數據。我得到下面的錯誤。
Error: Uncaught (in promise): TypeError: Cannot read property 'title' of null
TypeError: Cannot read property 'title' of null
at Object.eval [as updateRenderer] (ng:///AppModule/UserProfilePage.ngfactory.js:273:28)
at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:8100/build/main.js:12978:21)
at checkAndUpdateView (http://localhost:8100/build/main.js:12357:14)
at callViewAction (http://localhost:8100/build/main.js:12667:17)
at execComponentViewsAction (http://localhost:8100/build/main.js:12613:13)
我正在使用與GraphQL後端數據相關的Apollo客戶端。我想用Apollo來獲取數據... – Purus