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?
– user20212
There is no error, when I try to edit it appears on the page:
Cannot POST /usuarios/edit/5596819e5410f3a422b5aa7c
– DiegoAugusto
when I try to log the model into Else nothing appears either.
– DiegoAugusto
is empty or does not reach the point?
– user20212
There is a route /users/Edit/:id of type POST?
– user20212
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 pageedit
– DiegoAugusto
Does changing the route to POST work?
– DiegoAugusto
So, in your form you’re saying that the request is post: form(method="POST", action="/usuarios/Edit/#{value. _id}", role="form")
– user20212
I put as POST and it worked. Thanks for helping, put as answer so I can mark as correct
– DiegoAugusto
In the tutorial the guy put and worked, I think it worked because it was an old version
– DiegoAugusto