这是我的密码,
function insert_data
接收一个对象数组,并在其中循环,然后将其存储在数据库中
function connect_to_mysql(callback){
var con = mysql.createConnection(config.db);
con.connect(function(err) {
if (err){
callback(true , null );
} else {
callback(false , con );
}
});
}
function insert_data(insert_data_stack){
connect_to_mysql( function(err,conn) {
if(!err)
{
for( var ikay in insert_data_stack )
{
var query_data = insert_data_stack[ikay];
console.log( ' inserting ---------------->' + ikay );
console.log( query_data);
var query = conn.query('INSERT INTO transactions SET ?', query_data, function(err, result) {
if (err)
{
console.log(err.sqlMessage) }
else
{
console.log(' >>>> INSERTED >>>>> ' + query_data.trace_code );
}
});
}
// conn.end();
}
});
}
这是问题所在,在这一行
console.log(' >>>> INSERTED >>>>> ' + query_data.trace_code );
我只得到最后一个对象的reace_代码
这是我的输出
inserting ---------------->0
{ amount: '1000',
payment_time: 'âª20',
full_cart: '6274121185233616',
trace_code: '045330',
account_id: 5,
dig4_cart: '3616' }
inserting ---------------->1
{ amount: '1000',
payment_time: 'âª20',
full_cart: '6274121185233616',
trace_code: '045138',
account_id: 5,
dig4_cart: '3616' }
inserting ---------------->2
{ amount: '1000',
payment_time: 'âª20',
full_cart: '6274121185233616',
trace_code: '044868',
account_id: 5,
dig4_cart: '3616' }
>>>> INSERTED >>>>> 044868
>>>> INSERTED >>>>> 044868
>>>> INSERTED >>>>> 044868
为什么我只得到最后一个物体
trace_code
?