Reputation: 203
I started developing Typescript & C# not long ago, while formerly most of my development was with javascript+php. I am developing an angular 2 app, and created an HTTP service. The problem I am discussing here has more to do with Typescript than with angular 2.
in the following code:
export class HttpRest{
constructor(
private _http: Http,
private userIdsx: UserIds,
private _jsonFormater: JsonFormater
){}
...
getGroupsList(){
return this._http.get('file.json')
.map(res => res.json());
}
...
}
I am passing an anonymous lambda function with a single statement, and i want to add curly braces to create a function scope, but when I do that things get broken.
Im trying to refactor:
.map(res => res.json());
To:
.map((res) => {
res.json()
});
So I could do more operations, but this brakes the code and results in an undefined error on the JSON object that is defined by res.json().
The question is, why does adding curly braces brakes this simple function? Isn't lambda functions with scope curly braces a built in feature of the language?
Upvotes: 3
Views: 462
Reputation: 202256
Use the following. You need to return something.
.map((res) => {
return res.json()
});
With the concise body version (without curly brackets), it's implicitly done...
Upvotes: 4