Reputation: 2637
I got this error handler code in Angular-11:
return this.api.post('auth/user/login', data, headers).subscribe(
(result: any) => {
console.log(result.message);
console.log(result.code);
if (result.error === 'true' ){
console.log(result.message);
}
},
data => this.tokenHandler(data),
error => this.errorHandler(error.error)
);
}
errorHandler(error: any){
this.notify.clear();
// console.log(error);
if (error.errors && error.errors.username){
this.error = error.errors.username;
}
else if (error.message === 'Unauthorized'){
this.error = null;
this.notify.error('Invalid Login Details or email not confirmed', {timeout: 0})
} else {
this.error = null;
this.notify.error(error.message, {timeout: 0})
}
}
But it resulted into these errors:
Error: src / app / auth.component.ts: 84: 11 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '(error: any) => void'
is not assignable to parameter of type '() => void'.
84 error => this.errorHandler(error.error) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules / rxjs / internal / Observable.d.ts: 54: 5
54 subscribe(next ? : (value: T) => void, error ? : (error: any) => void, complete ? : () => void): Subscription;
~~~~~~~~~
The last overload is declared here.
Error: src / app / auth.component.ts: 84: 11 - error TS7006: Parameter 'error'
implicitly has an 'any'
type.
84 error => this.errorHandler(error.error)
This line is highlighted: error => this.errorHandler(error.error)
How do I get it resolved?
Thanks
Upvotes: 0
Views: 1736
Reputation: 127
It sounds to me like errorHandler
is getting called without an argument. Perhaps the error being passed to it has no error
property? (Meaning error.error
is undefined?)
Upvotes: 0
Reputation: 5118
This explains it subscribe(next ? : (value: T) => void, error ? : (error: any) => void, complete ? : () => void): Subscription;
You're passing three arguments (callbacks) to subscribe
, yes. However, your third callback, the error handler, doesn't match the third argument of the above function definition. It accepts a callback with no parameters as the third argument.
Instead, it looks like the first callback should get the token from the result (result.data
?) and call this.tokenHandler
there, and the second callback should be your error handler. Bacially getting rid of data => this.tokenHandler(data),
Upvotes: 1