I refer to this post which was really helpful to understand Observables, but there is still something I don't get.
In the plunker it refers to, I changed the "src/myfreinds.ts" to see a bit what happen and added some console log:
First I left it as the same:
export class FriendsList{
result:Array<Object>;
constructor(http: Http) {
console.log("Friends are being called");
http.get('friends.json')
.map(response => {
console.log("map");
console.info(response);
response.json(); //MISTAKE WAS HERE SHOULD HAVE HAD "return response.json()" INSTEAD AS EXPLAINED IN THE ANSWER GIVEN
})
.subscribe(result => {
console.log("subscribe");
console.info(result);
this.result =result;
}
);
}
}
In that case I can see in the log that result
beneath subscribe
is undefined
. But response
beneath map
contains the response indeed.
If I take away the map
part and keep the subscribe
part like this:
export class FriendsList{
result:Array<Object>;
constructor(http: Http) {
console.log("Friends are being called");
http.get('friends.json')
.subscribe(
result => {
console.log("subscribe");
console.info(result);
this.result =result;
}
);
}
}
This time, in the console I can see beneath subscribe the details of the result
and not undefined
like I had in previous case.
I'd like to understand why subscribe, success function does not contain everytime the result whether map
function is applied or not?