-1

Please just help clarify whats happening inside these two function. I'm a bit more comfortable with mongoose as well. If you can provide a mongoose a equivalent that would be great as well.

router.put('/api/todos', function (req, res){
    db.todos.update({
        _id: mongojs.ObjectId(req.body._id)
    }, {
        isCompleted: req.body.isCompleted,
        todo:req.body.todo
    }, {}, function (err, data){
        res.json(data);
    });
});

router.delete('/api/todos/:_id', function (req, res){
    db.todos.remove({
        _id: mongojs.ObjectId(req.params._id)}, '',
        function (err, data){
            res.json(data);
        });
    });
OJay
  • 4,763
  • 3
  • 26
  • 47
JET
  • 13
  • 1
  • 7

1 Answers1

0

router.put('/api/todos', function (req, res){ db.todos.update({ _id: mongojs.ObjectId(req.body._id) }, { isCompleted: req.body.isCompleted, todo:req.body.todo }, {}, function (err, data){ res.json(data); }); }); This takes the request body from a PUT request to /api/todos. It ues the _id from the request body to find a document in MongoDB and sets isCompleted to the value of isCompleted from the request body and todo to the value of todo from the request body. When the update completes, it sends the resultant object as a json response.

router.delete('/api/todos/:_id', function (req, res){ db.todos.remove({ _id: mongojs.ObjectId(req.params._id)}, '', function (err, data){ res.json(data); }); }); This takes a DELETE request to /api/todos/{some id}, deletes the corresponding document in Mongo, and returns what that document was to the client.

Josh C.
  • 4,303
  • 5
  • 30
  • 51