The way to make such judgments directly in mysql is to use row_count(), which is followed by the SQL statement you execute. Nodejs' i/o is asynchronous, so this creates a problem, which is not easy to judge Which sentence row_count() is the result of SQL execution? I glanced at the document briefly, and the document did not describe this problem. I wanted to nest functions to achieve synchronization, but I accidentally discovered that it was in the asynchronous function corresponding to SQL. There is an affectedRows field in the parameters. After testing, this is the result of row_count().
Example: Copy the code as follows:
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
});
});