简体   繁体   English

尝试使用 PUT 路由更新字幕 (node.js/express)

[英]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.我正在尝试通过使用update.ejs页面上的表单来更新我的帖子表中的标题列。

For some reason, it just redirects me back to my index.ejs page without updating my database.出于某种原因,它只是将我重定向回我的index.ejs页面,而不更新我的数据库。 Can anyone spot anything wrong with my routes or ejs page?任何人都可以发现我的路线或 ejs 页面有什么问题吗?

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 :这是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:您使用 http 方法POST提交 html 表单,在您的快速应用程序中,您使用.put作为您的路线,因此您应该将后者更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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