When I was playing node. js in my spare time, I encountered some pain points. When I used the mysql module to connect to mysql, I wanted to know the update and delete statements, if the delete statement is successful, you can use row_count () instead of directly making such a judgment in mysql. This statement must be followed by the SQL statement you run. node. js's I/o operations are asynchronous, which leads to a problem. It is not good to judge which SQL statement row_count () is executed. after a rough glance at the document, the document does not describe the problem. if you want to implement the synchronization through function nesting, you have no intention of finding that the affectedRows field is included in the parameters of the asynchronous function corresponding to SQL Execution. After testing, this is the result of row_count.
Instance:
The Code is 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': 'failed ',
'Msg ': msg,
'Data': null
});
Return;
}
Res. send ({
'Code': 200,
'State': 'success ',
'Msg ': 'updated ',
'Data': null
});
});