Raymond17
Raymond17

Reputation: 73

TypeError: argument callback must be a function when provided in Node js with a SQL query

Here is the code :

 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é ! "
        });
  }
  });



});

i think it's the syntax of my query but i don't know how to write it correctly .

Here is the error :

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)

Thank's in advance !

Upvotes: 1

Views: 8148

Answers (1)

abondoa
abondoa

Reputation: 1783

I suspect that you might be using the connection.query incorrectly. Maybe check up on the docs for that (https://github.com/mysqljs/mysql#performing-queries). You are providing it with 5 arguments, but it should only have at most 3. Try the following:

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é ! "
            });
        }
    });
});

Upvotes: 5

Related Questions