TypeError:在Node js中提供SQL查询时,参数回调必须是一个函数

时间:2020-03-26 00:00:11

标签: mysql node.js express

这是代码:

 application.post('/Modification/SaveEdit/:idd',function(req,res){
 var mem = req.body.memo;

 connection.query('UPDATE memos SET textMemo = ?',mem,'WHERE idMemo = ?',req.params.idd, function (error, results, fields){
        if (error) {
    console.log("error ocurred",error);
    res.send({
      "code":400,
      "failed":"erreur survenue"
    })
  }else{
    console.log('Resultats: ', results);
    res.send({
      "code":200,
      "success":"votre memo est bien modifié ! "
        });
  }
  });



});

我认为这是查询的语法,但是我不知道如何正确编写它。

这是错误:

TypeError: argument callback must be a function when provided
at Function.createQuery (/home/mohand/node_modules/mysql/lib/Connection.js:57:13)
at Connection.query (/home/mohand/node_modules/mysql/lib/Connection.js:181:26)
at /home/mohand/TpsWEB/DMajour/expreServer.js:146:13
at Layer.handle [as handle_request] (/home/mohand/node_modules/express/lib/router/layer.js:95:5)
at next (/home/mohand/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/mohand/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/mohand/node_modules/express/lib/router/layer.js:95:5)
at /home/mohand/node_modules/express/lib/router/index.js:281:22
at param (/home/mohand/node_modules/express/lib/router/index.js:354:14)
at param (/home/mohand/node_modules/express/lib/router/index.js:365:14)

先谢谢!

1 个答案:

答案 0 :(得分:1)

我怀疑您可能错误地使用了connection.query。也许检查一下文档(https://github.com/mysqljs/mysql#performing-queries)。您为其提供了5个参数,但最多只能有3个参数。尝试以下操作:

application.post('/Modification/SaveEdit/:idd',function(req,res){
 var mem = req.body.memo;

 connection.query('UPDATE memos SET textMemo = ? WHERE idMemo = ?',[mem,req.params.idd], function (error, results, fields){
        if (error) {
    console.log("error ocurred",error);
    res.send({
      "code":400,
      "failed":"erreur survenue"
    })
  }else{
    console.log('Resultats: ', results);
    res.send({
      "code":200,
      "success":"votre memo est bien modifié ! "
        });
  }
  });



});