无法使用Node JS从表单数据获取

时间:2020-09-08 12:16:08

标签: javascript node.js postgresql ejs

我正在尝试从模态内的表单中获取输入并更新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>

0 个答案:

没有答案