Edit NodeJs data

2

I have a Crud NodeJs where I can add data to the database, but now I wanted to edit this data and the tutorial I follow is a bit old, in this tutorial editing is done by PUT my JADE page looks 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 stays 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);

Can anyone help?

    
asked by anonymous 03.07.2015 / 20:27

2 answers

0

Change the route method to POST, to learn more about http: link

[] 's

    
07.07.2015 / 21:13
1

As you want a PUT, you need the method-override module

And there you go:

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

Formerly the method-override was part of the expression, but in version 4, it was separated into other middleware. Do this with the? _Method = PUT and install the method-override. Dai yes, it should work and you can do the PUT.

    
18.12.2015 / 00:28