mysql - INSERT doesn't trigger right away -


what do:

  1. select database,
  2. if row not exist
  3. insert row
  4. 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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -