Angular 4 api call turn response to json returns undefined

I follow suit and they make a call to a local web api like this.

return this.http.get("http://localhost:26264/api/news").map((response: Response)=>{
  response.json();
});

If you observe the response value before the call .json(), everything looks fine.

Then i do it

var data = this.authService.Login(value.Email, value.Password).subscribe((res: any)=>{
    console.log(res);
  });

At this point, res is undefined? Ignore the fact that I'm calling the login method for the api news controller. I changed the api endpoint, because before that I was getting other errors.

+4
source share
2 answers

Consider the change:

.map((response: Response)=>{
  response.json();
});

to

.map((response: Response) => response.json())

The reason for this is ..

.map(response: Response => response.json() )

says it creates an anonymous function and takes a response object and returns the json method from the response object (which returns an object serialized as JSON)

.map((response: Response)=>{
  response.json();
});

, , json , .

:

.map((response: Response)=>{
  return response.json();
});
+4

.

return this.http.get("http://localhost:26264/api/news").map((response: Response)=>{
  response.json();
});

( ). Response . :

myJsonifyFunc(response: Response) {
  response.json();
  // notice there is no return statement!
}
return this.http.get("http://localhost:26264/api/news").map(myJsonifyFunc);

:

.....map((x,y,z) => {...})

x, y, z {...}. , "return" , void ( , undefined).

.....map((x,y,z) => ...)

, . , ... .

.....map((x,y,z) => ...)
.....map((x,y,z) => {return ...})

, , ... - .

, .

0

Source: https://habr.com/ru/post/1679368/


All Articles