mysql - INSERT doesn't trigger right away -
what do:
- select database,
- if row not exist
- insert row
- repeat step 1, select row step 3
expected result: after 4 steps there result in case
result: there no row in select, row exists (see through phpmyadmin)
mysqlpool.getconnection(function(err, connection) { connection.query('select * x key = ? limit 1', [ data.key ], function(err, result) { if (result.length == 0) { // works, verified through phpmyadmin: connection.query('insert x set ?', { key: data.key }); } }); connection.query('select * x key = ? limit 1', [ data.key ], function(err, result) { if (result.length != 0) { // expecting here, result[0] undefined?! } else { // end here? wtf } }); connection.release(); });
does node-mysql need kind of commit;
or something?
what doing wrong?
i believe due asynchronous nature of node, need call 2nd select in callback after inserting. following
mysqlpool.getconnection(function(err, connection) { connection.query('select * x key = ? limit 1', [ data.key ], function(err, result) { if (result.length == 0) { // works, verified through phpmyadmin: connection.query('insert x set ?', { key: data.key }, function(err, result){ secondselect(); }); } else secondselect(); }); });
Comments
Post a Comment