I'm currently using nodeJS to truncate a list of 60 tables in my database.
To do this, I'm using this code:
var mysql = require('mysql');
var connexion = mysql.createConnection({
host : '1.1.1.1',
user : 'user',
password : 'password',
database : 'db'
});
connexion.connect();
var tableList = [
'table0',
'table1',
'table2',
...
];
console.log('\n### Truncating tables... ###');
var i;
for(i = 0; i < tableList.length; i++){
connexion.query('TRUNCATE '+tableList[i]+';', function(err, rows, fields){
if(err) throw err;
console.log(i+'===> '+tableList[i]+' truncated.')
});
}
The problem is that the output is always:
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
I have two problems, the tableList array is not accessible within the callback function, and the callback function keeps thinking i=60 is the correct value.
What am I doing wrong?
I would like to have something like:
0===> table0 truncated.
1===> table1 truncated.
...