After tirelessly searching through various online resources like Google and Stack Overflow to troubleshoot my code without success, I find myself resorting to posting a question here. The issue at hand involves two nearly identical functions in my service class that make post requests to the backend API. Strangely, one function works perfectly fine while the other seems to trigger an immediate "status: 400" response without even executing properly on the frontend.
In the backend/API:
[HttpPost("Patients/update")] //not functioning
public async Task<IActionResult> UpdatePatientAsync(Patient editedPatient)
{
try
{
_logger.LogDebug("APIController.UpdatePatientAsync() was called...");
var updated = await _dbHandler.UpdatePatientAsync(editedPatient);
if (updated)
{
return Ok(updated);
}
else
{
return BadRequest("Patient not updated!");
}
}
catch
{
throw;
}
}
[HttpPost("Patients/comment/update")] //works perfectly!
public async Task<IActionResult> UpdatePatientCommentAsync(PatientComment editedComment)
{
try
{
_logger.LogDebug("APIController.UpdatePatientComment() was called...");
var updated = await _dbHandler.UpdatePatientCommentAsync(editedComment);
if (updated)
{
return Ok(editedComment);
}
else
{
return BadRequest("Comment not updated.");
}
}
catch
{
throw;
}
}
In my service:
updatePatient(editedPatient: Patient): Observable<Patient> { //malfunctioning
return this.http.post<Patient>(ConfigService.Config.apiBaseUrl + "/Patients/update", editedPatient).pipe(
catchError(this.rHndlr.handleError("updatePatient", this.updatedPatient))
)
}
updatePatientComment(editedComment: PatientComment): Observable<PatientComment> { //working flawlessly!
return this.http.post<PatientComment>(ConfigService.Config.apiBaseUrl + "/Patients/comment/update", editedComment).pipe(
catchError(this.rHndlr.handleError("updatePatientComment", this.updatedComment))
)
}
The problem arises when calling these functions:
updatePatient(updatedPatient: Patient): Promise<Patient> {
this.loading = {
loadingText: "Updating patient",
errorText: "Comment update failed, try something else.",
errorTextVisible: false
}
const promise = new Promise<Patient>((resolve, reject) => {
this.patientSvc.updatePatient(updatedPatient).subscribe({ //Not working as expected!!!
next: (data: Patient) => {
if (JSON.stringify(updatedPatient) === JSON.stringify(data)) {
console.log("Success updating patient!")
}
},
error: (err) => {
alert("Error updating patient data!\n" + JSON.stringify(err));
},
complete: () => {
resolve(this.patient);
}
})
});
return promise;
}
updatePatientComment(editedComment: PatientComment): Promise<PatientComment> {
this.loading = {
loadingText: "Updating comment",
errorText: "Comment update failed, try something else.",
errorTextVisible: false
}
const promise = new Promise<PatientComment>((resolve, reject) => {
this.patientSvc.updatePatientComment(editedComment).subscribe({ //Working smoothly!!!
next: (data: PatientComment) => {
if(JSON.stringify(editedComment) === JSON.stringify(data)){
console.log("Success updating comment!");
this.commentChanged = false;
}
},
error: (err) => {
alert("Error updating comment! \n" + JSON.stringify(err));
},
complete: () => {
resolve(this.patientComment);
}
})
});
return promise;
}
These are the objects involved:
export interface Patient {
id: number;
socialSecurityNumber: string;
firstName: string;
lastName: string;
diagnosisId: number;
riskAssessmentId: number;
deceasedDate?: number;
commentId: number;
clinicId: number;
active: boolean;
staffId: number;
}
export interface PatientComment {
id: number,
commentText: string,
commentDate: Date,
signature: string
}
I am baffled by the discrepancy between the two similar functions and would appreciate any insights or solutions you might have regarding what could be causing this issue. Could it possibly be related to the size of the Patient object? Your assistance will be greatly valued!