【问题标题】:Trying to update captions with a PUT route (node.js/express)尝试使用 PUT 路由更新字幕 (node.js/express)
【发布时间】:2020-12-06 16:21:15
【问题描述】:

我正在尝试通过使用update.ejs 页面上的表单来更新我的帖子表中的标题列。

出于某种原因,它只是将我重定向回我的index.ejs 页面,而不更新我的数据库。谁能发现我的路线或 ejs 页面有什么问题?

这是我的更新路线:

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('/')
  })
})

这里是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>

【问题讨论】:

    标签: javascript node.js database express ejs


    【解决方案1】:

    您使用 http 方法 POST 提交 html 表单,在您的 express-app 中您使用 .put 作为您的路线,因此您应该将后者更改为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 2019-09-02
      相关资源
      最近更新 更多