I can not edit in CRUD - NodeJS + Express + MongoDB

0

Following:

I started literally today working with NodeJS + Express + MongoDB and as an exercise I started with the famous CRUD (Create, Read, Update, Delete).

I was able to do almost anything but edit, I'm really "beating myself" to solve, a good time and I decided to ask for help.

Editing code

// Editar item
router.put('/edit', function(req, res) {
    var id = req.body.id;
    var title = req.body.title;
    var description = req.body.description;
    model.findById(id, function(error, task) {
        if (error) {
            throw error;
        }
        task.title = title;
        task.description = description;
        task.save(function() {
            res.redirect('/');
        });
    });
});

I'm really having trouble finding the error since I do not handle almost any of this syntax, which is new to me, below the form in html (ejs)

<form action="/add" method="post">
    <legend>Incluir uma tarefa</legend>
    <input type="text" name="title" placeholder="Título">
    <textarea name="description" placeholder="Descrição"></textarea>
    <button type="submit">Adicionar</button>
</form>

I'm using the template ejs.

Thank you for helping me!

    
asked by anonymous 21.06.2017 / 03:13

1 answer

0

Your route should look like this

router.put('/edit/:id', function(req, res) {
var id = req.params.id;
.......

ai in your form would be like this

<form action="/edit/idDaTask" method="post">
<input type="hidden" name="_method" value="put" />
    
31.01.2018 / 18:54