Problems with routers node + express

0

I have a sequence of methods (get, post, put, delete), however the put method is wrong and I do not know how to solve it.

This is the snippet of code:

router.route('/')
    .get((req, res) => res.status(200).send('Lista de Produtos'))
    .post((req, res) => res.status(201).send(req.body))
    .put(':id',(req, res, next) => {
        const id = req.params.id;
        res.status(201).send({
            id: id,
            item: req.body
        });
    })
    .delete((req, res) => res.send('Remove Produtos'))

And this is the error:

  

Error: Route.put () requires a callback function but got a [object String]

I need the url in the url to send an id such as: link

The id would be 123 ..

and the answer would be {    "id": "123",    "item": {    } }

    
asked by anonymous 26.09.2018 / 22:13

1 answer

0

Good afternoon Vitor,

The example I use to create CRUD's in Node is this here, link

I suggest you take a look at how it does the direct req.params and the callback function that it puts in case of error

 exports.update_a_task = function(req, res) {
     Task.findOneAndUpdate({_id: req.params.taskId}, req.body, {new: true}, function(err, task) {
         if (err)
             res.send(err);
         res.json(task);
     });
 };

The Task.findOneAndUpdate comes from this part here

    var mongoose = require('mongoose'),
    Task = mongoose.model('Tasks');

I hope I have helped!

    
26.09.2018 / 23:10