Nodejs USES the mysql module to get the number of rows that are updated and deleted

  • 2020-03-30 02:22:18
  • OfStack

In mysql directly to such a judgment method is to use row_count (), the statement clung to behind you execute the SQL statement. The Nodejs are asynchronous I/o and this creates a problem, not too good judgment row_count () what the results of the SQL execution. Rough glanced in the document, the document does not describe the problem. This want to achieve synchronization by nested function, but not found in the corresponding asynchronous execute SQL function parameters in   The affectedRows field is tested as the result of row_count().
Example:

var cmd = 'UPDATE users SET ' + field + ' = ' +  value + ' WHERE id = ' +  userid;
  console.log(cmd);
  db.query(cmd, function(err, rows, fields){
    var affectedRows = rows.affectedRows;
    if(err || affectedRows){
      var msg = 'update ' + field + ' error';
      logger.error(msg); 
      res.send({
        'code': 500,
        'state': 'failure',
        'msg': msg,
        'data': null
      });
      return;
    }
    res.send({
      'code': 200,
      'state': 'success',
      'msg': 'updated',
      'data': null
    });
  });


Related articles: