简体   繁体   English

Angular:Observable,订阅不将数据返回到Component类

[英]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.

相关问题 返回数据以从可观察的角度订阅 - Returning data to subscribe from observable in angular 通过在 Angular 中返回字符串而不是 Observable 来消除 subscribe(...) - Eliminating subscribe(...) by returning string instead of Observable in Angular 如何订阅子组件angular2中的observable? - how to subscribe to observable in child component angular2? 如何在 Angular 的不同子组件中订阅 Observable - How to Subscribe Observable in different Child component in Angular 订阅 angular 9 组件上的可观察更改的适当位置 - proper place to subscribe to observable changing on angular 9 component Angular7服务-返回包含来自另一个可观察对象的数据但包含该组件中第二个可观察对象的数据的可观察对象? - Angular7 service - returning observable that contains the data from another observable but subscribing to that second Observable within component? Angular observable 使用订阅检索数据 - Angular observable retrieve data using subscribe Angular - 将数据订阅到可观察的返回未定义 - Angular - subscribe data to observable returns undefined Angular 组件中基于不同数据的组件之间单次/多次公共 Observable 订阅的最佳方法 - Best Approach for single/multiple time common Observable subscribe between components based on different data stream in Angular component 试图在服务中创建可观察对象,在组件中使用它,在另一个组件中订阅它,角度为4 - trying to create an observable in a service, use it in a component and the subscribe it in another component angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM