function read_account(csv, index) {
if (index == csv_json.length) return;
let line = csv_json[index][0].split(';');
let block = csv_json[index];
var account = line[0];
connection.query('SELECT id FROM account WHERE account_number = ' + account,
function(error, results, fields) {
if (error)
throw error;
console.log(results);
if (results == null || typeof results == undefined || results.length == 0) {
console.log("ENTREI");
connection.query('INSERT INTO account (account_number) VALUES (' + account + ')',
function(err, results, fields) {
callback(err, "1 account inserted!")
if (err)
throw err;
});
}
});
};
您可以考虑为select和insert编写两个不同的函数:
function select(callback){
connection.query('SELECT id FROM account WHERE account_number = ' + account, function(error, results, fields) {
if (error) {
return callback(error);
} else {
return callback(null, results, fields);
}
});
}
function insert(results, fileds, callback){
connection.query('INSERT INTO account (account_number) VALUES (' + account + ')',
function(err, results, fields) {
if (err){
return callback(err);
} else {
return callback(null, "1 account inserted!")
}
});
}
function read_account(csv, index, callback) {
if (index == csv_json.length) return;
let line = csv_json[index][0].split(';');
let block = csv_json[index];
var account = line[0];
select(function(null, results, fields){
if(err){
return callback(err);
} else {
insert(results, fileds, callback);
}
});
}