[英]get method returns empty object in angular
Im using Api and it returns empty object.我使用 Api,它返回空的 object。 the console value is {}, my code is given below
控制台值为 {},我的代码如下
component:零件:
isValidZipCode() {
this.appService.getData().subscribe(
(response: Result) => this.result = response,
(err) => console.log(err)
);
console.log(this.result);
}
service:服务:
getData(): Observable<Result> {
return this.http.get<Result('https://www.wsjwine.com/api/address/zipcode/12345');
}
Observable is async. Observable 是异步的。 So When u console app out of subscribe you may not still get data from server.
因此,当您控制台应用程序退出订阅时,您可能仍无法从服务器获取数据。 You need to do it in subsribe
你需要在订阅中完成
isValidZipCode(){
this.appService.getData().subscribe(
(response: Result) => {
// do it here
console.log(this.result);
},
(err) => console.log(err)
);
}
Modify your isValidZipCode method as follows修改您的 isValidZipCode 方法如下
isValidZipCode(){
this.appService.getData().subscribe(
response => {
if (response['response'].statusMessage == 'successful') {
this.result = response['response']
console.log(this.result);
console.log('success')
}
else console.log('error')
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.