I'm setting up a search call in my app and I'm getting the following error:
Type 'Observable' is not assignable to type 'Observable'. Type 'User' is not assignable to type 'User[]'. Property 'length' is missing in type 'User'.
Those messages are clear and I understand them, but I don't understand why I'm getting them. Here is the relevant code.
searchResults: Observable<Array<User>>;
constructor(fb:FormBuilder, private _userService: UserService){
this.currentUser = JSON.parse(localStorage.getItem('user'));
this.searchResults = this.searchTerm.valueChanges.debounceTime(400).distinctUntilChanged()
.switchMap(searchTerm => this.userSearch(searchTerm));
};
private userSearch(searchTerm: string): Array<User>{
this._userService.getUsers(this.currentUser.UserId, this.currentUser.Token).then(
data => {
//loader.dismiss();
if (data) {
return data as Array<User>;
} else {
//This really should never happen
console.error('Error retrieving users data: Data object is empty');
}
},
error => {
//loader.dismiss();
console.error('Error retrieving users data');
console.dir(error);
return error as Array<User>;
}
);
return new Array<User>();
}
To me the types should match up, but they obviously don't and I'm stumped as to why. My userSearch function returns an array of Users which means the the Observable should be of that type also, but somehow its coming back as a single user. Any help is appreciated.