I've encountered a strange issue with my Angular app where the delete request is not functioning as expected without any visible errors. Here's the code snippet from my service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class TodoService {
todoUrl = 'https://example.herokuapp.com/api/todoDB/';
constructor(private http: HttpClient) { }
getTodo() {
return this.http.get(this.todoUrl);
}
postTodo(todoObject: any) {
return this.http.post(this.todoUrl , todoObject);
}
deleteTodo(id: any) {
const url = `${this.todoUrl}${id}`;
console.log(url); // *** This is printing correct URL
return this.http.delete(url);
}
}
Although getTodo() and postTodo() functions are working perfectly, the deleteTodo() method seems to be malfunctioning without any error indications. Strangely, manually testing the URL logged in console.log(url) using Postman works fine but it fails when called from the app. In my component, I'm using the following code to trigger the deleteTodo() method of my service:
removeTodo(i: any) {
this.todoService.deleteTodo(this.todoArray[i]._id);
}
This is the delete route on my server:
// Delete Todo
router.delete('/:id' , (req , res) => {
Todo.findById(req.params.id)
.then((todo) => todo.remove().then(() => res.json({success : true})))
.catch(err => res.json({success : false}).status(404))
});