I have a function to delete employees like so
removeEmployee(empRefId: string): Observable<void>{
const docRef = doc(this.firestore, 'empDetails/'+empRefId+'123'); //123 to corrupt Id
const promise = deleteDoc(docRef);
return from(promise);
}
I call this from my component like so :
this.firebaseEmpService.removeEmployee(empRefId).subscribe({
next: (result) => {
console.log("succesfully deleted emp in db");
this.toastrService.success("Deleted emp successfully ");
},
error: (error) => {
console.error("Something went wrong:", error);
this.toastrService.error("Something went wrong");
},
complete: () => {
console.log("On complete delete");
// Handle completion cases
}
});
When supplying an incorrect ID, I would have thought I will get an error. However, the error
property of subscribe is never called.
I have surrounded the deletion call with try/catch and catch doesn’t catch the promise’s rejection – if it rejects at all.
try{
const promise = deleteDoc(docRef);
console.log("Returning from try");
return from(promise);
} catch(error)
{
console.log("Error in try catch");
console.log(error);
}
return of();
The documentation doesn’t say what happens when an incorrect Id is supplied – am only guessing the promise rejects. So, what am I doing wrong? Does deleteDoc
not return an error? With what I have currently, the promise and observable both complete successfully, how do I tell the UI that an incorrect ID was passed?
I have tried to use the promise as is, but the catch block never executes:
this.firebaseEmpService.removeEmployeePromise(empRefId)
.then((o) => {
this.toastrService.success("Deleted emp successfully ");
})
.catch((o) => {
console.log("REJECTED", o);
this.toastrService.error("Something went wrong");
});