简体   繁体   English

Observables - Angular - .map 不工作,但 .subscribe 可以

[英]Observables - Angular - .map is not working but .subscribe does

I have an endpoint which is called and looks like this:我有一个调用的端点,如下所示:

organizationSubsidiaries(sfid: string) {
  const url = `${this.baseURL}/${ENDPOINTS.organizations}/${this.organization.id}/${ENDPOINTS.subsidiaries}`;
  return this.http.get(url).map(res => res.json());
}

If I call it like this it works and consoles correctly:如果我这样称呼它,它就可以正常工作并且控制台正常:

getOrgSubsidiaries(id) {
  return this.organizationSubsidiaries(id)
    .subscribe(data => { //changing this to .map means it won't work!

      let subsidiaryNames = []
      data.forEach(sub => {
        console.log(sub.SubsidiaryName)
        subsidiaryNames.push(sub.SubsidiaryName)
      })
      subsidiaryNames = subsidiaryNames.filter(this.onlyUnique);

      console.log('data from subsidiaries' + JSON.stringify(subsidiaryNames))

    });
}

When it's .map it doesn't work, but I need it to be .map as I need to subscribe to it in another file so I can asynchronously use the data in another component:当它是.map它不起作用,但我需要它是.map因为我需要在另一个文件中subscribe它以便我可以异步使用另一个组件中的数据:

ngOnInit(): void {
  this.engagementService.getOrgSubsidiaries(id).subscribe(data => {
     this.orgSubsidiaries = data;
});

I need it so the .map also works as the subscribe does so I can actually subscribe to it and use the data in my other component!我需要它,所以.map也像subscribe一样工作,所以我可以实际subscribe它并在我的其他组件中使用数据!

In a slightly different issue this function in my other component doesn't recognise id.在一个稍微不同的问题中,我的其他组件中的此函数无法识别 id。

use the pipe in rxjs 6在 rxjs 6 中使用管道

 return this.http.get(url).pipe( map(res => res.json()));

import the map导入地图

import { map } from 'rxjs/operators'

The issue depends on two things:这个问题取决于两件事:

  1. Which version of Angular you're using.您使用的是哪个版本的 Angular。
  2. Whether you're using HttpClient or Http无论您使用的是HttpClient还是Http

Let's talk about the Angular Version first:先说Angular版本:

If you're using Angular 6, it comes with Rxjs 6. So on any Observable type value, you won't get to use operators like map directly by importing them from their paths and chaining them to the Observable value.如果您使用的是 Angular 6,则它带有 Rxjs 6。因此,在任何Observable类型值上,您将无法通过从它们的路径导入它们并将它们链接到Observable值来直接使用像map这样的运算符。 In this case, you'll have to use .pipe that would be available on the Observable value and inside this .pipe you can then use your Operators.在这种情况下,你将不得不使用.pipe这将是可在Observable值,这里面.pipe那么你可以使用你的运营商。

An eg:例如:

import { map } from 'rxjs/operators';
...
return this.http.get('...').pipe(map(...))  // This returns an Observable too

If you're using Angular 5, 4, or 2, it comes with previous versions of Rxjs.如果您使用的是 Angular 5、4 或 2,则它带有 Rxjs 的早期版本。 So on any Observable type value, you will get to use operators like map directly by importing them from their paths and chaining them to the Observable value.因此,在任何Observable类型值上,您都可以通过从它们的路径导入它们并将它们链接到Observable值来直接使用像map这样的运算符。 In this case, you'll not have to use .pipe在这种情况下,您不必使用.pipe

An eg:例如:

import 'rxjs/add/operators/map';
...
return this.http.get('...').map(...)  // This returns an Observable too

Now, let's talk about whether you're using HttpClient or Http :现在,让我们谈谈您使用的是HttpClient还是Http

If you're using HttpClient , then you won't have to map the response by calling .json on it.如果您使用的是HttpClient ,那么您将不必通过调用.json来映射响应。 So you can simply do:所以你可以简单地做:

constructor(private http: HttpClient, ...) {}
...
return this.http.get(...); // This will return the Actual Response DATA

If you're using Http , then you'll have to map the response by calling .json on it.如果您使用的是Http ,则必须通过调用.jsonmap响应。

So in case of Angular 2,4,5, it would look something like:所以在 Angular 2,4,5 的情况下,它看起来像:

import 'rxjs/add/operators/map'
...
constructor(private http: Http, ...) {}
...
return this.http.get(...).map(res => res.json());

And in case of Angular 6, I guess Http has been removed.在 Angular 6 的情况下,我猜 Http 已被删除。 So you won't be able to inject it as a dependency in the first place.因此,您将无法首先将其作为依赖项注入。

UPDATE更新

I think the issue is that you haven't injected Http or HttpClient as a Dependency in your EngagementService service.我认为问题在于您没有在EngagementService服务中注入HttpHttpClient作为依赖项。 And hence the error.因此错误。

In case of Angular 6:在 Angular 6 的情况下:

Just add a constructor as well, that injects HttpClient as a dependency.只需添加一个constructor ,将HttpClient作为依赖项注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM