Home:ALL Converter>nodejs mysql query doesn

nodejs mysql query doesn

Ask Time:2014-08-06T17:59:42         Author:user3913203

Json Formatter

I want to check if some date exist in a table, if not I want to insert it. I have done this in other project and there it works but now i don't know why it doesn't work.

var mysql = require('mysql');
var connection = mysql.createConnection({
   host     : '',
   user     : '',
   password : '',
   database : ''
});

[..]

connection.query('SELECT id FROM users [...]', function(err, results) {
    if (err) {
         throw err;
    } else if (results.length==1) {
         callback(null, results[0].id);
    } else {
         console.log('before insert');
         connection.query('INSERT INTO users SET ?', user, function(err, result) {
             console.log('insert');
             if (err) throw err;
         });
    }
});

The query with INSERT doesn't work, but if i get that query out of the SELECT query then it works. Doesn't matter if it is INSERT or other query. In console I only see: 'before insert' and no error. This query it's in a loop.

Author:user3913203,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25157411/nodejs-mysql-query-doesn
yy