简体   繁体   中英

Trying to update captions with a PUT route (node.js/express)

I'm trying to update the caption column in my post table through using a form on an update.ejs page.

For some reason, it just redirects me back to my index.ejs page without updating my database. Can anyone spot anything wrong with my routes or ejs page?

Here are my update routes:

app.get('/update', (req, res) => {
  console.log(res.locals.alerts);
  res.render('update', { alerts: res.locals.alerts });
});

// update caption
app.put('/update', isLoggedIn, (req, res) => {
  console.log('--- PUT route ---');
  // console.log(req.body.id)
  // const id = req.body.id
  db.post.update({
    caption: req.body.caption
  }, {
    where: { id: req.body.id }
  }).then(() => {
    res.redirect('/')
  })
})

Here is the update.ejs :

<form action="/update?_method=put" method="POST" enctype="multipart/form-data">
    <input type="text" value="<%= post.id %>" name="caption" placeholder="New Caption:" id="updatePostText" >
    <input type="submit" class="btn btn-primary" id="postSubmit" value="Update">
</form>

Your submitting an html form using the http method POST , in your express-app you are using .put for your route, so you should change the latter to:

app.post('/update', isLoggedIn, (req, res) => { ... }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM