Reputation: 185
I am trying to update position in my todo list
var Todo = mongoose.model('Todo', {
text : String,
done : Boolean
});
database
mode has two elements
, and I want to update only boolean
- done.
app.put('/api/todos/:todo_id', function(req, res) {
var query = { _id: req.params.todo_id };
Todo.update(query,
{ done: true
}, function(err, todo) {
if (err)
res.send(err);
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});
});
I would like to check actual value and then update. Done is from mongoose
model
so I can't check it like that?
if (done != true){
done: true }
else {
done: false }
Is something like that possible?
Upvotes: 0
Views: 900
Reputation: 11950
app.put('/api/todos/:todo_id', function(req, res) {
Todo.findById(req.params.todo_id, function(err, todo) { // get todo by Model.findById
if (err) return res.send(err);
todo.done = !todo.done; // toggle todo's done field
todo.save(function(err) { // save to database
if (err) return res.send(err);
res.json(todo); // return the saved todo
});
});
Upvotes: 1