首页 > 解决方案 > 无法使用节点 js 从表单数据中获取

问题描述

我正在尝试从模式内的表单中获取输入并更新 postgres 中的输入字段。但是当我试图获取输入字段时,结果是未定义的。

router.post('/changePassword', (req, res) => {
  const emailReq = req.session.email;
  const oldpasswordReq = req.body.password;
  const newpasswordReq = req.body.newpassword;
  if (oldpasswordReq !== newpasswordReq) {
    const salt = bcrypt.genSaltSync();
    const hash = bcrypt.hashSync(req.body.newpassword, salt);
    knex('users').where({ email: emailReq })
      .update({ password: hash })
      .then(() => {
        res.redirect('/');
      });
    res.render('/', { title: 'Password changed'});
  } else { (alert('The passwords are same')); };
});

模态中的表单在ejs文件中如下所示

<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Change Password</h4>
</div>
<div class="modal-body"><br><br>
<form method="post" action="/users/changePassword">
<div class="form-group">
<label for="usr">Old Password:</label>
<input type="password" class="form-control" id="oldpassword" name="oldpassword">
</div>
<div class="form-group">
<label for="pwd">New Password:</label>
<input type="password" class="form-control" id="newpassword" name="newpassword">
</div>
<button type="submit" class="btn btn-default" name="upload">Confirm</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>

标签: javascriptnode.jspostgresqlejs

解决方案


推荐阅读