Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'void' is not assignable to type 'User[]'

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();
   })
 }
}

enter image description here

like image 893
Omar Abdelhady Avatar asked Sep 14 '25 00:09

Omar Abdelhady


2 Answers

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
   })
}
like image 58
Amit Chigadani Avatar answered Sep 16 '25 18:09

Amit Chigadani


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();
 })
like image 32
Sajeetharan Avatar answered Sep 16 '25 20:09

Sajeetharan