Okay so once the user logs in, i am storing their details in req.user. The user should then be able to edit their details (fistname, lastname, email) in a form rendered with the /profile route as seen below:
app.get('/profile', checkAuthenticated, (req, res) => {
res.render('profile.ejs', req.user);
})
Here is the form:
<form action="/update-profile" method="post">
<div >
<input id="firstname" name="firstname" type="text" value="<%=firstname%>" required />
<label for="firstname">Firstname</label>
</div>
<div >
<input id="lastname" name="lastname" type="text" value="<%=lastname%>" required />
<label for="lastname">Lastname</label>
</div>
<div >
<input id="email" name="email" type="email" value="<%=email%>" required />
<label for="email">Email</label>
</div>
<button type="submit">Update</button>
</form>
After submitting the form the i want the user to be redirected to the /update-profile route, where i can perform an update to the database (MySQL).
app.put('/update-profile', (req, res) => {
console.log(req.params)
console.log(req.body)
res.redirect('/')
})
However I'm recieving a "Cannot POST /update-profile" error.
I was just going to use the user_id stored in req.user, and new user data (firstname, lastname, email) stored in req.body.params to update the database, but i've clearly gotten a bit mixed up with my routes.
Any help would be greatly appreciated, thanks so much for your time!
CodePudding user response:
It seems you are listening to a PUT request in your express app, but sending a POST.
app.post('/update-profile', (req, res) => {
console.log(req.params)
console.log(req.body)
res.redirect('/')
});
If you change your code to this, it should work.