0

I am trying to write a PUT request for my API. I'm using restify to build the API. I keep getting the errors, could you please help?

    //PUT (Update) Items

    app.put('/items/:item_id', function(req, res){

    var query = Item.where({_id: req.params.item_id});
    query.findById(req.params.id, function (err, items) {
    item.name = req.body.name;
    item.description = req.body.description;
    item.url = req.body.url;

    req.item.save(function (err) {
        if (!err) {
            console.log("updated");
        } else {
            console.log(err);
        }
        res.send(204, item);
    });
});

I tried testing it using POSTMAN client and I get this error

     {
         "code": "InternalError",
         "message": "Object #<Query> has no method 'findById'"
     }

Thanks.

Kokoliko
  • 1
  • 2
  • 1
    What errors are you receiving? – mfitzp Feb 23 '15 at 12:50
  • @mfitzp I tried testing it using POSTMAN client and I get this error { "code": "InternalError", "message": "Object # has no method 'findById'" } – Kokoliko Feb 23 '15 at 13:29
  • `findById` is a mongoose convenience method. If you are using the native `mongodb` driver then you will have to use `find({_id:req.params.id})` – ZeMoon Feb 23 '15 at 13:38

1 Answers1

0

The findById method internally maps to find with the parameter {_id: }

You can use either:

Item.find({_id: req.params.item_id}, function (err, items) {

});

or

Item.findById(req.params.item_id, function (err, items) {

});

Both are the same thing. The reason is because when you write

 var query = Item.where({_id: req.params.item_id});

A query object is returned, on which the findById method cannot be called. It can only be called on the Item model.

If you are trying to combine queries, I would suggest combining them at the filter level or use aggregation.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98