【发布时间】:2019-10-25 20:21:57
【问题描述】:
在尝试订阅服务文件传递的响应时,我得到一个未定义的值
这是我的代码
service.ts
getUserDetails(): Observable<GetUserByIdModel> {
const token = this._storeService.getStoredData().id_token;
const candidateId = this._storeService.getStoredData().profile.CandidateId;
const headers = new HttpHeaders()
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json');
const options = { headers: headers };
const url = `${this.candidateUrl}/Get/${candidateId}`;
this._http.get<GetUserByIdModel>(url, options).subscribe((resp) => {
this.userDetails = {
firstname: resp.firstname,
lastname: resp.lastname,
nationalityId: resp.nationalityId,
identityTypeId: resp.identityTypeId,
identityValue: resp.identityValue,
titleId: resp.titleId,
genderId: resp.genderId,
candidateContactChannels: [
{ type: 1, value: resp.candidateContactChannels[0].value },
{ type: 2, value: resp.candidateContactChannels[1].value }
],
};
console.log(this.userDetails);
})
return new Observable((observer) => {
observer.next(this.userDetails);
observer.complete();
});
}
component.ts
ngOnInit() {
this._loader.start();
this._profileService.getUserDetails().subscribe((resp) => {
this.user = resp;
console.log(this.user);
this._loader.stop();
}, (error) => {
this._loader.stop();
this._errorService.openErrorPopup('Failed to get user data.');
});
}
我所指的未定义值在 component.ts 文件 console.log(this.user); 我的服务中的 console.log 正确显示填充的数据
【问题讨论】:
-
如果console.log(this)完成而不是console.log(this.user),你能告诉console中显示的内容吗?
-
然后我得到
MyDetailsComponent {_profileService: ProfileService, _confirmationService: ConfirmationService, _loader: LoaderService, _errorService: ErrorService, _storeService: StoreService, …} user: undefined
标签: angular