Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialshelley barak
1,522 PointsUpdate Mongodb with node js and express
Hello After doing the tutorial "User Authentication With Express and Mongo" https://teamtreehouse.com/library/user-authentication-with-express-and-mongo I am trying to add an edit page where I can change the name field. But I am having problems with the routing, can anyone please help? here is the routing I added:
router.get('/edit', mid.requiresLogin, function(req, res, next) {
User.findById(req.session.userId)
.exec(function (error, user) {
if (error) {
return next(error);
} else {
return res.render('edit', { title: 'edit', name:user.name, user:user._id });
}
});
});
router.put('/edit', mid.requiresLogin, function(req, res) {
user.findByIdAndUpdate({_id: req.params.id},
{
name: req.body.name
});
});
And here is the edit.pug:
extends layout
block content
.main.container.clearfix
h1 Editing #{name}'s profile!
form(method="POST", action="/edit")
input(type="hidden", name="_method", value="PUT")
p Name:
input#name.form-control(type='text', value='#{name}')
p
input(type="submit")
3 Answers
Seth Kroger
56,413 PointsAs you've found, browsers can only make GET and POST requests. It require a trick of two to make a PUT or DELETE request, but these methods need to be supported on the server-side to work. Express doesn't support overriding the HTTP verb by default. That functionality is left to, you guessed it, middleware. You can add a middleware called method-override to do what you want. It parses the _method=PUT out of the url, and not the POST body be default, but the README shows how to do the latter.
In app.js:
var methodOverride = require('method-override')
// // override with POST having ?_method=PUT
app.use(methodOverride('_method'))
Then in edit.pug:
extends layout
block content
.main.container.clearfix
h1 Editing #{name}'s profile!
//- We add _method=PUT to the URL instead of a hidden input.
form(method="POST", action="/edit?_method=PUT")
p Name:
input#name.form-control(type='text', value=name name="name")
p
button.btn.btn-primary(type="submit") Save
shelley barak
1,522 PointsI got it to work. I had also an issue with the routing. Here is the fixed routing:
router.put('/edit', function(req, res) {
User.findByIdAndUpdate(req.session.userId,
{name: req.body.name},
function(err, response){
console.log(response);
res.redirect('/edit');
});
});
Thank you Seth Kroger
Mike Lundahl
13,510 PointsI'm trying to do the same. But using react. Not working for me for some reason.. Anything special to think about using react instead of pug?