[英]Angular: Observable, Subscribe not returning data to Component class
I am having issues with Observables service layer invocation, I am getting data till the service layer but not propagating to component layer not sure what was wrong. 我遇到了Observables服务层调用的问题,我收到的数据直到服务层,但没有传播到组件层,不知道出了什么问题。 Am I doing something wrong here? 我在这里做错了吗?
In HelperUtils.ts (This is the same class for the http REST API) 在HelperUtils.ts中(这是与http REST API相同的类)
getResource(url: string): Observable<any[]> {
this.headers.set('token', token);
this.options = new RequestOptions({ headers: this.headers });
console.log('URL at HttpUtils', this.getServer() + url);
console.log('Options at HttpUtils', this.options);
return this.http
.get(this.getServer() + url, this.options)
.map(this.retrieveData)
.catch(this.handleException);
}
In DNSService.ts 在DNSService.ts中
getDNS(): Observable<Dns[]> {
return this.helperUtils
.getResource('databases')
.subscribe(
res => this.data = res,
// make sure you get data here.
// console.log('Data from Service API layer', this.data),
error => console.log('Error from backend API', +error),
() => console.log('Done')
);
}
In the DNSComponent.ts 在DNSComponent.ts中
result: Dns[];
this.dnsSvc
.getDNS()
.subscribe(
res => {
console.log('In component result class:===>' + res);
this.result = res;
console.log('In component class:===>' + this.result);
},
// make sure you get data here.
// console.log('Data from API layer', +res);
error => console.log('Error from backend API', +error),
() => console.log('Done')
);
DNSModel.ts DNSModel.ts
export class Dns {
public dnsname: string;
}
updated code below : 更新的代码如下 :
model.ts model.ts
export class Dns {
public dnsname: string;
}
helperUtils.ts helperUtils.ts
// - Simply return the observable of the call
getResource(url: string): Observable<Response> {
this.headers.set('Api_key', api_key);
this.options = new RequestOptions({ headers: this.headers });
console.log('URL at HelperUtils', this.getServer() + url);
console.log('Options at HelperUtils', this.options);
return this.http.get(this.getServer() + url, this.options);
}
Service layer 服务层
data: Dns[];
// Subscribe to the result and do the Mapping.
getDNS(): Observable<Dns[]> {
return this.httpUtils.getResource('databases')
.map(res => {
if (res.ok) {
let body = this.retrieveData(res);
return body.map(x => new Dns());
}
// console.log('Issue with the response');
}
);
}
private retrieveData(res: Response) {
console.log('Response at httpService -', res.json());
let body = res.json();
console.log('Response at httpService res body-', body);
return body;
}
Component.ts Component.ts
result: Dns[];
ngOnInit() {
this.instanceDetailsSvc.getDNS()
.map(res => {
this.result = res;
},
error => console.log('Error from backend API', +error)
);
console.log('The Result Is:::::' + this.result);
}
On your getDNS()
method, you are assigning the result of the getResource
call to `this.result, but you are not returning the result itself as an observable.: 在你的getDNS()
方法中,你将getResource
调用的结果getDNS()
`this.result,但是你没有将结果本身作为一个observable返回:
getDNS(): Observable<Dns[]> {
return this.helperUtils
.getResource('databases')
.subscribe(
res => this.data = res, // <= HERE
error => console.log('Error from backend API', +error),
() => console.log('Done')
);
}
From the code in your component I see that you are doing the API call to receive an array of objects of type DNS
. 从组件中的代码我看到您正在进行API调用以接收DNS
类型的对象数组。 What you need to do in this case is map the result of the call to DNS[]
and subscribe to it from the component. 在这种情况下,您需要做的是将调用结果映射到DNS[]
并从组件中订阅它。 Something like this: 像这样的东西:
//- Simply return the observable of the call
getResource(url: string): Observable<any[]> {
this.headers.set('token', token);
this.options = new RequestOptions({ headers: this.headers });
return this.http.get(this.getServer() + url, this.options);
}
... ...
// Subscribe to the result and do the Mapping.
getDNS(): Observable<Dns[]> {
return this.helperUtils.getResource('databases')
.map(res => {
if (res.ok) {
// do mapping logic here, i.e.
return res.json.map(x => new DNS(x));
}
else {
//handle invalid result
}
}
);
}
Finally, on your component: 最后,在您的组件上:
this.dnsSvc.getDNS()
.subscribe(res => {
this.result = res;
},
error => console.log('Error from backend API', +error));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.