I am unable to edit in CRUD - Nodejs + Express + Mongodb

Asked

Viewed 166 times

0

next:

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

I managed to do almost everything except the editing part, I’m really "beating myself" to solve, a long time ago and 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 don’t know almost anything about this syntax, which is new to me, follows below the html form (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 ejs template.

I appreciate anyone who can help me!

  • 2

    as you have router.put('/edit', ..., shouldn’t be <form action="/edit" method="put">?

  • 1

    CRUD means: Create, Read, Update, Delete

  • @merchant already tried but does not change anything, it is as if he did not find what I am doing in the code of Edit because it takes me to a page of error 404.

1 answer

0

your route should be like this

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

Ai in your form would be so

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.