In a MERN CRUD app with JWT authentication I am trying to use PATCH request to edit the text field in my MongoDB documents. But it behaves weirdly.
Another PATCH request that changes the status boolean field works perfectly fine. It makes a request with a todoId, an empty object and Auth header.
However, in the Network tab I can see that the request for text which is practically the same but with different URL ('/edit' added) and a non-empty object behaves like a GET request to http://localhost:3000/api/todos/ with 304 OK status for some reason. But in Postman it works as intended.
It is supposed to be, for example, http://localhost:3000/api/todos/62bae4bdda66c364463c58d0/edit. A working patch request to status is to: http://localhost:3000/api/todos/62bae4bdda66c364463c58d0
The issue is probably syntax, so please take a look at the code for both patch requests, in each file top is for status (working), then for text (not working):
Frontend:
Dispatching in TodoCard.js from TodoSlice.js (editedTodo is a String, so {editedTodo} is an object with editedTodo key)
// status
const handleStatusUpdate = () => {
dispatch(updateStatus(todo._id))
}
// text
const handleEdit = () => {
dispatch(editTodo(todo._id, {editedTodo}))
}
In async functions in try-catch blocks in TodoSlice.js:
// status
return await todoService.updateStatus(id, token)
// text
return await todoService.editTodo(id, editedTodo, token)
Axios requests in TodoService.js:
// status
const response = await axios.patch(API_URL + todoId, {}, config)
// text
const response = await axios.patch(API_URL + todoId + '/edit', editedTodo, config)
Finally, Backend, routes in todoRoutes.js (protect is auth middleware):
// status
router.patch('/:id', protect, updateStatus)
// text
router.patch('/:id/edit', protect, editTodo)
And todoController.js
// status
const updatedTodo = await Todo.findByIdAndUpdate(req.params.id, { status: !todo.status }, { new: true })
// text
const editedTodo = await Todo.findByIdAndUpdate(req.params.id, { text: req.body.editedTodo }, { new: true })
One is working, other is not. Again, the issue is probably syntax. I would really appreciate any help since I've been stuck on this for some time. Thank you in advance!