I am facing an issue with my Angular application where I need to handle server-side failures when sending a list of strings to a backend server. Despite following tutorials, I am struggling to display error messages on the client side in case something goes wrong.
The angular component.ts file includes an upload method:
constructor(
private readonly SVC: UploadService) { }
uploadStringlist(): void {
let sarray: string[] = [];
sarray.push("string1");
sarray.push("string2");
this.SVC.uploadList(sarray).subscribe(
_ => { this.finished,emit(`protocols sent: next`)},
err => { this.finished.emit(`Protocols sent: error` + err)},
() => { this.finished.emit(`Protocols sent`)});
}
and the upload.service.ts file has the following method:
uploadList(slist: string[]): Observable<string> {
return this.http.post<string>(apiurl, slist, httpOptions)
.pipe(
tap(res => log("uploaded protocol list to server", res)),
catchError(handleError<string>("test")));
}
On the server side, there is an uploadController:
[HttpPost]
public IActionResult Post(string[] slist)
{
foreach (var s in slist)
{
// do something
}
return StatusCode(409);
//return Ok();
}
Regardless of what is returned from the controller, the result received on the client side is always "protocols sent." Ideally, I would like to receive a list of results for each entry in the string array or at least an error message if one occurs on the server side. However, I am unsure how to implement this.