'PUT request crashes the server
I'm getting this error when trying to update an entry (in this case, checking a todo and setting it to true/false) It seems like i'm getting the proper object (seen in the data console log) which makes me wonder why i'm getting [object Object].
Would appreciate any kind of help!
Here's the action :
export function onSaveTodo(todo) {
return async (dispatch) => {
try {
const savedTodo = await todosService.saveTodo(todo);
const action = {
type: 'UPDATE_TODO',
todo: savedTodo,
};
dispatch(action);
} catch (err) {
console.log('cant save todo');
}
};
}
the service :
const BASE_URL = 'todo';
async function saveTodo(todo) {
try {
if (todo._id) {
return httpService.put(BASE_URL, todo);
} else {
return httpService.post(BASE_URL, todo);
}
} catch (err) {
console.log(`could not save todo `, err);
throw err;
}
}
Http service :
export const httpService = {
get(endpoint, data) {
return ajax(endpoint, 'GET', data);
},
post(endpoint, data) {
return ajax(endpoint, 'POST', data);
},
put(endpoint, data) {
console.log('endpoint:', endpoint);
console.log('data:', data);
return ajax(endpoint, 'PUT', data);
},
delete(endpoint, data) {
return ajax(endpoint, 'DELETE', data);
},
};
The backend controller :
async function updateTodo(req, res) {
try {
const { todo } = req.body;
// console.log('todo:', todo);
const savedTodo = await todoService.update(todo);
res.json(savedTodo);
} catch (err) {
logger.error('Failed to update todo', err);
res.status(500).send({ err: 'Failed to update todo' });
}
}
and the backend service :
async function update(todo) {
try {
const newTodo = {
...todo,
_id: ObjectId(todo._id),
};
console.log('newTodo:', newTodo);
const collection = await dbService.getCollection('todo');
await collection.updateOne({ _id: newTodo._id }, { $set: newTodo });
return todo;
} catch (err) {
logger.error(`Can not update toy ${todo._id}`, err);
throw err;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

