I got this error in visual studio code:
Argument of type '(query: string) => void | Observable' is not assignable to parameter of type '(value: string, index: number) => ObservableInput'. Type 'void | Observable' is not assignable to type 'ObservableInput'. Type 'void' is not assignable to type 'ObservableInput'
Check code:
public searchVal(val: string) {
this.suggestions = (new Observable((observer: Observer<string>) => {
observer.next(val);
})).pipe(
switchMap((query: string) => {
if (query) {
return this.returnApiCall(query); //THIS IS WORK WITH ONLY ONE RETURN
}
return of([]);
})
);
}
BUT this is no work with if :
public searchVal(val: string) {
this.suggestions = (new Observable((observer: Observer<string>) => {
observer.next(val);
})).pipe(
switchMap((query: string) => {
if (query) {
if(this.serverApi === 'Driver'){
return this.getAllDrivers(query);
}else if(this.serverApi === 'Vehicle'){
return this.getVehicle(query);
}
}
return of([]);
})
);
}
when I try to return some other api- calls does not work?
My api call is :
returnApiCal(query: string) {
return this.accountsService.getDrivers(query)
.pipe(
map((data: any) => {
return data.body || [];
}),
tap(() => noop, err => {
this.errorMessage = err && err.message || 'Something goes wrong';
})
)
}
In this line is red line :
switchMap((query: string) => { //here
Make sure that your switchMap's callback function allways returns something:
public searchVal(val: string) {
this.suggestions = (new Observable((observer: Observer<string>) => {
observer.next(val);
})).pipe(
switchMap((query: string) => {
if (query) {
if(this.serverApi === 'Driver'){
return this.getAllDrivers(query);
}else if(this.serverApi === 'Vehicle'){
return this.getVehicle(query);
}
else { // you need to provide else here
// return an observable when the two above conditions were not satisfied
}
}
return of([]);
})
);
}
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