undefined response returned from observable/http/async call in angular
I am trying to get some data from server by making an http call to server which returns an observable. But it always returns undefined. What is the problem?
Service:
@Injectable()
export class ForumService {
constructor(private http: Http) {}
getForumList(): Observable < any > {
let headers = new Headers({
'Content-Type': 'application/json'
});
let headerOptions = new RequestOptions({
headers: headers
});
return this.http.get("http://localhost:8080/forums", headerOptions).map((res) => res.json()).catch((err) => err)
}
}
Component:
@Component({
...
})
export class EventComponent {
myEvents: any;
constructor(private es: ForumService) {}
ngOnInit() {
this.es.getForumList().subscribe((response) => {
this.myEvents = response;
});
console.log(this.myEvents); //This print undefined!
}
}