如何清除此错误:UnhandledPromiseRejectionWarning: TypeError: callback is not a function

时间:2021-06-13 23:22:35

标签: mysql node.js express

当我继续处理来自 html 表单的 post 请求时,我的代码抛出此错误

UnhandledPromiseRejectionWarning: TypeError: callback is not a function .

我可以知道我应该做哪些修改来解决这个问题吗?

app.post('/profile', function (req, res) {
    var useremail = req.body.email;
    var password = req.body.psw;
    if (useremail && password) {
        dbconnect.query('SELECT * FROM customerdetails WHERE email = ? AND pws = ?', [useremail, password], function(error, results) {
      if ((results.length)== 0) {
        console.log(results.length)
        res.redirect('/error')
        //res.status(401).render('login',{msg:'Incorrect credentials'});
            } 
      else {
        //console.log(results[0].CHname);
        const id = results[0].CHID;
        //console.log(id);
        const token = jwt.sign({ id }, process.env.JWT_SECRET, {
          expiresIn: process.env.JWT_EXPIRES_IN
        });
  
        const cookieOptions = {
          expires: new Date(
            Date.now() + process.env.JWT_COOKIE_EXPIRES_IN * 1 * 5 * 60 * 1000
          ),
          httpOnly: true
        };
        res.cookie('biopay', token, cookieOptions);
        //console.log(req.cookies.jwt)
       dbconnect.query('SELECT * FROM carddetails WHERE CHID = ? ', [id], function(error, results1) {
          if ((results.length)== 0) {
            res.redirect('/error')
            //res.status(401).render('login',{msg:'Incorrect credentials'});
          } 
          else{
            console.log(results1)
            res.status(200).render('profile', { msg:results[0].CHname},{tables: results1})
        
          }
        })
        
            }           

1 个答案:

答案 0 :(得分:0)

答案是 res.status(200).render('profile', { msg:results[0].CHname},tables: results1});

这是由于按照 res.render() 语法作为回调传递的表的值