node.js mysql failing to insert

I'm messing around with node.js, using the Faker, underscore, mysql and randy libraries to add some test data to a web app.

So far so good-- but in one of my loops, the mysql call fails every time, but it also fails to generate any errors. I'm a bit stumped:

var sql = 'SELECT id FROM mytable WHERE 1';
client.query(sql, function(err, rows, fields){
    // loop through results
    _.each(rows, function (v, i){
        // get the insert id
        var id = v.id; 
        console.log (id); // prints valid ID            
        // generate other data
        var stuff_count = randy.randInt(0,12);
        _(stuff_count).times(function(n){
            var sql = 'INSERT INTO other_table (linked_id, item_id) VALUES ('+id+','+randy.randInt(1,50)+')';
            console.log(sql); // prints sql
            client.query(sql, function(err, rows, fields) {
              console.log(sql); // prints nothing
              if (err){
                console.log(sql); // prints nothing
                throw err; // prints nothing
              }
        });
    });
});

The looping logic is working fine and it gets all the way to where the sql should execute the INSERT against the other_table-- but tailing the mysql log shows nothing hitting the DB, and none of the debug statements inside the client.query block are printing anything, on success or failure.

assuming you are just running this standalone, your code probably finishes before it has a chance of doing the inserts.

place a setTimeout(..., 10000) after the _(stuff_count).times(...)

By using _().times(), you queue all the inserts, but are not waiting for completion. you should use a control flow library to ensure you wait for all inserts to complete.