So I'm trying to make a put request using
My put form
<form id="for" action="/people" method="post">
<div class="">
<input type="text" name="Name" value=<%= data[0].name %> >
</div>
<div class="">
<input type="text" name="City" value="" placeholder=<%= data[0].favorite_city %>>
</div>
<input type="hidden" name="_method" value="put"/>
<input type="submit" name="" value="Edit">
</form>
My server side scripts
var methodOverride = require('method-override'); app.use(methodOverride('_method')); app.put('/people', function(req,res){ pg.connect(DATABASE_URL, function(err, client, done){ var pid = parseInt(req.params.id); var name = req.query.Name; var city = req.query.City; var client_query = ''; if(name = ''){ client_query = `update persons set favorite_city =${city} where id = ${pid}`; } else{ client_query = `update persons set name = ${name}, favorite_city= ${city} where id= ${pid}`; } client.query(client_query, function(err, result){ res.redirect(`/people/${pid}`) done(); pg.end(); }) }) });
I am using method override but it only seems to do a post with the new data The problem is similar with delete so I'm not going to put the code here because it may be a little redundant. But if anyone wants to see it I don't mind
methodOverride('_method')
returns a middleware that looks for the key _method in the query string (see method-override docs ). By including the _method variable in the form and sending the form with the POST method, _method is not included in the query string, but in the request body. Probably you want to use the body-parser middleware before the methodOverride middleware and then do something like:
app.use(methodOverride(req => req.body._method));
In addition, you probably want to replace query
with body
in:
var name = req.query.Name;
var city = req.query.City;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.