I have theses method that I use to upload images to my backend in a project using Angular 8 and I never had problems. Recently I started a new project using angular 10 and try to use theses method again, but I'm getting several errors and I'm having a hard time making it work correctly.
I can ignore the errors, and everything works fine but I'm trying to make it correctly clean.
Can someone give some light on this problem?
Thanks in advance.
In my service I have:
uploadLogo(file: File): Observable<HttpEvent<{}>> {
const formData: FormData = new FormData();
formData.append('file', file!);
const req = new HttpRequest('POST', `${this.resourceUrl}/upload`, formData, {
reportProgress: true,
responseType: 'text',
});
return this.http.request(req);
}
and on my component.ts I have:
upload(): void {
this.loadingImage = true;
this.currentFileUpload = this.selectedFiles.item(0);
this.eventService.uploadLogo(this.currentFileUpload).subscribe(
(res: HttpResponse<any>) => this.onSaveSuccess(res.body),
(res: HttpErrorResponse) => this.onSaveError(res)
);
}
private onSaveSuccess(result: any): void {
this.result = result;
}
private onSaveError(result: any): void {
this.jhiAlertService.error(result.message, null, '');
}
on my method upload() in the line:
(res: HttpResponse<any>) => this.onSaveSuccess(res.body),
i'm getting:
TS2769: No overload matches this call.
Overload 1 of 2, '(next: null | undefined, error: (error: any) => void, complete?: (() => void) | undefined): Subscription
', gave the following error.
Argument of type '(res: HttpResponse<any>) => void
' is not assignable to parameter of type 'null | undefined
'.
Type '(res: HttpResponse<any>) => void
' is not assignable to type 'null
'.
Overload 2 of 2, '(next?: ((value: HttpEvent<{}>) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription
', gave the following error.
Argument of type '(res: HttpResponse<any>) => void
' is not assignable to parameter of type '(value: HttpEvent<{}>) => void
'.
Types of parameters 'res
' and 'value
' are incompatible.
Type 'HttpEvent<{}>
' is not assignable to type 'HttpResponse<any>
'.
Read more here: https://stackoverflow.com/questions/65704636/why-im-getting-no-overload-matches-this-call
Content Attribution
This content was originally published by Tanino at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.