I can not understand this error , why it won't allow me to post data to users array so i can access it from the html ?
this is the component
export class Service1Component implements OnInit {
selectedUser: User;
users: Array<User>;
constructor(private _userService: UserService) { }
ngOnInit() {
this._userService.getUsers()
.subscribe(resUserData => {
this.users = resUserData
});
}
onSelectUser(user:any){
this.selectedUser = user;
}
}
this is the service i want to subscribe to
export class UserService {
private _getUrl = '/api/users'
constructor(private _http: Http) { }
getUsers(){
return this._http.get(this._getUrl)
.map((response: Response) => {
response.json();
})
}
}
You have missed to return from your function which is considered as void
in your component.
getUsers(){
return this._http.get(this._getUrl)
.map((response: Response) => {
return response.json(); // add return statement
})
}
Just initialize your users
users : User[] = [];
EDIT
After editing your question, i see you have missed to return the response in your service
return this._http.get(this._getUrl)
.map((response: Response) => {
return response.json();
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With