Javascript 此._callback.apply不是函数!节点js Mysql错误

Javascript 此._callback.apply不是函数!节点js Mysql错误,javascript,mysql,node.js,Javascript,Mysql,Node.js,控制器代码 app.post('/savedata', function(req, res) { var cope = req.body; console.log("On server side"); console.log(cope.Client_ID); var queries = connection.query('update lv_billing.client SET Client_ID = ?, Client_Name = ?,Status = ?,Updt_Time = ?,Up

控制器代码

app.post('/savedata', function(req, res) {
var cope = req.body;
console.log("On server side");
console.log(cope.Client_ID);
var queries = 
connection.query('update lv_billing.client SET Client_ID = ?, Client_Name = ?,Status = ?,Updt_Time = ?,Updt_By = ?,Updt_ID = ?,Cluster = ? where Client_ID = ?',cope.Client_ID,cope.Client_Name,cope.Status,cope.Updt_Time,cope.Updt_By,cope.Updt_ID,cope.Cluster,cope.Client_ID, function(err,res){
if(err) throw err;
    console.log('Inserted!');
})
});

上面的代码抛出的错误“this.\u callback.apply”甚至不是函数。设置一个小上下文。我正试图用数组中的新值更新我的表。”cope'是一个数组,它保存需要更新的值

回调应该是第二个参数

connection.query('query string here', callback_function_here);
只需像这样连接变量,否则它们将被视为参数:

'update lv_billing.client SET Client_ID = ' + cope.Client_ID + ' ... '

我不确定您使用的是哪种mysql驱动程序,因为您没有这样说,也没有在编写的代码中提供任何线索。话虽如此,我认为代码应该更像这样:

app.post('/savedata', function(req, res) {
  var cope = req.body;
  console.log("On server side");
  console.log(cope.Client_ID);
  var queries = connection.query('update lv_billing.client SET Client_ID = ' + cope.Client_ID + ', Client_Name = ' + cope.Client_Name + ' ,Status = ' + cope.Status + ' ,Updt_Time = ' + cope.Updt_Time + ',Updt_By = ' + cope.Updt_By + ',Updt_ID = ' + cope.Updt_ID + ',Cluster = ' + cope.Cluster + ' where Client_ID = ' + cope.Client_ID, function(err,res){
  if(err) throw err;
    console.log('Inserted!');
  })
});
.query(sqlString, callback)
    app.post('/savedata', function(req, res) {
      var cope = req.body;
      console.log("On server side");
      console.log(cope.Client_ID);
      var params = [cope.Client_ID, cope.Client_Name, cope.Status, cope.Updt_Time, cope.Updt_By, cope.Updt_ID, cope.Cluster, cope.Client_ID];
      var queries = 
      connection.query('update lv_billing.client SET Client_ID = ?,Client_Name = ?,Status = ?,Updt_Time = ?,Updt_By = ?,Updt_ID = ?,Cluster = ? where Client_ID = ?',params, function(err,res){
       if(err) throw err;
       console.log('Inserted!');
      });
    });
通常,当你得到一个错误,说某个东西不是函数,这意味着你试图使用某个东西作为一个不是函数的函数。我的假设是,您使用的库与此类似:

请注意,查询的格式如下:

app.post('/savedata', function(req, res) {
  var cope = req.body;
  console.log("On server side");
  console.log(cope.Client_ID);
  var queries = connection.query('update lv_billing.client SET Client_ID = ' + cope.Client_ID + ', Client_Name = ' + cope.Client_Name + ' ,Status = ' + cope.Status + ' ,Updt_Time = ' + cope.Updt_Time + ',Updt_By = ' + cope.Updt_By + ',Updt_ID = ' + cope.Updt_ID + ',Cluster = ' + cope.Cluster + ' where Client_ID = ' + cope.Client_ID, function(err,res){
  if(err) throw err;
    console.log('Inserted!');
  })
});
.query(sqlString, callback)
    app.post('/savedata', function(req, res) {
      var cope = req.body;
      console.log("On server side");
      console.log(cope.Client_ID);
      var params = [cope.Client_ID, cope.Client_Name, cope.Status, cope.Updt_Time, cope.Updt_By, cope.Updt_ID, cope.Cluster, cope.Client_ID];
      var queries = 
      connection.query('update lv_billing.client SET Client_ID = ?,Client_Name = ?,Status = ?,Updt_Time = ?,Updt_By = ?,Updt_ID = ?,Cluster = ? where Client_ID = ?',params, function(err,res){
       if(err) throw err;
       console.log('Inserted!');
      });
    });

像这样使用您的参数。这对我来说很好。

正如@rugdaler提到的,您应该提供回调函数作为第二个或第三个参数。如果您使用的是mysql模块,您可以如下更改您的函数:

app.post('/savedata', function(req, res) {
  var cope = req.body;
  console.log("On server side");
  console.log(cope.Client_ID);
  var queries = connection.query('update lv_billing.client SET Client_ID = ' + cope.Client_ID + ', Client_Name = ' + cope.Client_Name + ' ,Status = ' + cope.Status + ' ,Updt_Time = ' + cope.Updt_Time + ',Updt_By = ' + cope.Updt_By + ',Updt_ID = ' + cope.Updt_ID + ',Cluster = ' + cope.Cluster + ' where Client_ID = ' + cope.Client_ID, function(err,res){
  if(err) throw err;
    console.log('Inserted!');
  })
});
.query(sqlString, callback)
    app.post('/savedata', function(req, res) {
      var cope = req.body;
      console.log("On server side");
      console.log(cope.Client_ID);
      var params = [cope.Client_ID, cope.Client_Name, cope.Status, cope.Updt_Time, cope.Updt_By, cope.Updt_ID, cope.Cluster, cope.Client_ID];
      var queries = 
      connection.query('update lv_billing.client SET Client_ID = ?,Client_Name = ?,Status = ?,Updt_Time = ?,Updt_By = ?,Updt_ID = ?,Cluster = ? where Client_ID = ?',params, function(err,res){
       if(err) throw err;
       console.log('Inserted!');
      });
    });
有关更多信息,请查看源: