I'm trying to create a todo-list app and checking it with POSTMAN on every type of request. GET and POST requests are working fine. But PUT and DELETE are not working as expected.
As expected means : It is not showing 404 neither showing results instead it's showing an error, i.e., Could not get any response.
Here's my code of put and delete :
var todos = [];
// DELETE /todos/:id
app.delete('/todos/:id', function(req,res){
var todoId = parseInt(req.params.id, 10);
var matchedTodo = _.findWhere(todos, {id: todoId});
if(!matchedTodo){
res.status(404).send();
}else{
todos = _.without(todos, matchedTodo);
res.json(matchedTodo);
//console.log(todos);
}
});
// PUT /todos/:id
app.put('/todos/:id', function(req,res){
var todoId = parseInt(req.params.id, 10);
var matchedTodo = _.findWhere(todos, {id: todoId});
var body = _.pick(req.body, 'description', 'completed');
var validAttributes = {};
if(!matchedTodo){
return res.status(404).send();
}
if(body.hasOwnProperty('completed') && _.isBoolean(body.completed)){
validAttributes.completed = body.completed;
}else if(body.hasOwnProperty('completed')){
return res.status(400).send();
}
if(body.hasOwnProperty('description') && _.isString(body.description) && body.description.trim().length > 0){
body.description = body.description.trim();
validAttributes.description = body.description;
}else if(body.hasOwnProperty('description')){
return res.status(400).send()
}
_.extend(matchedTodo, validAttributes);
res.json(matchedTodo);
});
Now, I don't know if it's a bug or something.