Edit data from Nodejs

Asked

Viewed 564 times

2

I have a Crud Nodejs where I can add data in the database, but now I wanted to edit this data and the tutorial I follow is a little old, in this tutorial the editing is done by PUT my JADE page is like this:

extends ../layout

block content
    h1 Alteração de Usuário
    hr
    br
    form(method="POST", action="/usuarios/edit/#{value._id}", role="form")
      input(type="hidden" name="_method" value="put")
      div(class="form-group")
        label Nome:
        input(type="text", name="nome", class="form-control", value="#{value.nome}")
      div(class="form-group")
        label Login:
        input(type="text", name="login", class="form-control", value="#{value.login}")
      input(type="submit", value="Atualizar", class="btn btn-success")
    br
    br
    a(href="/usuarios" title="Voltar") Voltar

And this is my update method that is in control:

update: function(req,res){
            Usuario.findById(req.params.id, function(err, data){
                if(err){
                    console.log(err);
                }else{
                        var model   = data;
                        model.nome  = req.body.nome;
                        model.login = req.body.login;
                        model.save(function(err){
                            if(err){
                                console.log(err);
                            }else{
                              res.redirect('/usuarios');
                            }
                        });
                }
            });
        }

And the route is this:

app.put('/usuarios/edit/:id', usuarios.update);

Anyone can help?

  • In the controller, is the first error returning something? If so, what? On Else, have you tried logging in the "model" and see how the fields are filled in? In callback, you are falling into error or success?

  • There is no error, when I try to edit it appears on the page: Cannot POST /usuarios/edit/5596819e5410f3a422b5aa7c

  • when I try to log the model into Else nothing appears either.

  • is empty or does not reach the point?

  • There is a route /users/Edit/:id of type POST?

  • the route is PUT type, in the tutorial I follow says that PUT is no longer used but needs to do a "gambiarra" like this: form(method="POST", action="/usuarios/edit/#{value._id}", role="form") this is on my page edit

  • Does changing the route to POST work?

  • So, in your form you’re saying that the request is post: form(method="POST", action="/usuarios/Edit/#{value. _id}", role="form")

  • I put as POST and it worked. Thanks for helping, put as answer so I can mark as correct

  • In the tutorial the guy put and worked, I think it worked because it was an old version

Show 5 more comments

2 answers

1

How do you want a PUT, you need the module method-override

And then yes:

form(method="POST", action="/usuarios/edit/#{value._id}?_method=PUT", role="form")

Formerly the method-override was part of the express, but in version 4, it was separated into another middleware. Do it this way with the ? _method=PUT and install the method-override. So yes, it should work and you can do the PUT.

0


Browser other questions tagged

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