What does .map do for an http?

3

I was doing a test service and from my component when calling the service and wanting to add ".sucribe" it gave me an error that the method was not an observable ...

Searching on the internet I managed to solve the problem if in my service I added something like this using .map:

loadAll() {
    return this.http.get(this.url)
        .map(res => res.json());            
}

When adding .map from the component if it worked well ".subscribe".

What I do not understand is what that .map does, search in google but I did not find info.

According to what the visual studio code shows me (in English) it is something that takes a result and creates an observable ... Could you please clarify what does this .map do?

Thank you very much

    
asked by Panchin 17.04.2018 в 16:39
source

2 answers

4

I recommend that you use the latest version of angular , since link is already obsolete and now link is used.

Example:

Version ANGULAR <= 4.2

http is used to consume apis; and what .map does is transform the result into JSON to be used in your component with subscribe. (in this version of angular if or if you must use .map )

loadAll() {
    return this.http.get(this.url)
        .map(res => res.json());            
}

Version ANGULAR >= 4.3

httpClient is used to consume apis; in this version it is no longer necessary to use .map , since internally it transforms the result into json by default; and in your component you do the same consuming with subscribe.

loadAll() {
    return this.http.get(this.url);                   
}

I'll give you the official documentation how you should implement httpclient to your project: link

    
answered by 17.04.2018 / 17:18
source
1

The behavior of .map in an observable is analogous to the behavior of .map in an array : Transform the elements into whatever you want using the function you pass.

For example, let's say you have an http call like your example:

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

I assume that this.http is an attribute of the class Http , so I warn you that its use is already considered outdated .

What happens here is that the call this.http.get(url) returns a Observable<Response> , that is, you have a Response object. But what you are interested in is the answer itself, a JSON string with your data. Therefore, with .map(...) you make a transformation: each object of type Response emitted is transformed into an object resulting from doing JSON.parse (...) to the text of the response.

I show you an example of how it works with an array:

let arrayTexto=['100','99'];

let transformaaNumeroYSumaCinco=function (texto) {
  return +texto + 5;
}

console.log(arrayTexto.map(transformaaNumeroYSumaCinco));

Now, as I mentioned before, this transformation is necessary because the Http.get method returns a Response, but it is considered outdated because in Angular v4.3 and later there is a class HttpClient that also has the methods get, post , put ... and directly return an object already parsed .

    
answered by 17.04.2018 в 17:28