gpt4 book ai didi

javascript - node.js 将数组传递给 mysql 回调

转载 作者:行者123 更新时间:2023-11-29 06:17:40 26 4
gpt4 key购买 nike

是否可以将数组传递到 mysql 回调中,例如:

connection.query('SELECT count(id) as wynik FROM used where asset_id = 
\''+asid+'\'', inventory[i],function(err,wiersze){
console.log(inventory[i]);
});

现在 inventory[i] 里面不可用,但我需要它。

最佳答案

由于您处于 for 循环中,因此您需要使用立即调用的匿名函数或简单的 .forEach()。两者都在下面作为示例提供。

立即调用匿名函数

for (var i = 0; i < inventory.length; i++) {
(function(i) {
if (inventory[i].market_hash_name == rows[j].real_name) {
var asid = inventory[i].assetid;
connection.query('SELECT count(id) as wynik FROM used where asset_id = \'' + asid + '\'', function(err, wiersze) {
console.log(inventory[i]);
process.exit(1);
if (wiersze[0].wynik == 0) {
var employee = {
asset_id: asid,
trans_id: rows[j].tid
};
connection.query('INSERT INTO used SET ?', employee, function(err, res) {
if (err) throw err;
offer.addMyItem(inventory[i]);
});
licznik++;
}

});

}
})(i);
});

.forEach()

inventory.forEach(function(item) {
if (item.market_hash_name == rows[j].real_name) {
var asid = item.assetid;
connection.query('SELECT count(id) as wynik FROM used where asset_id = \'' + asid + '\'', function(err, wiersze) {
console.log(item);
process.exit(1);
if (wiersze[0].wynik == 0) {
var employee = {
asset_id: asid,
trans_id: rows[j].tid
};
connection.query('INSERT INTO used SET ?', employee, function(err, res) {
if (err) throw err;
offer.addMyItem(item);
});
licznik++;
}

});

}
});

关于javascript - node.js 将数组传递给 mysql 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34966933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com