几个月前我问了几个关于这个问题的问题,最近我又回到了那个脚本。我想出了一些办法,一位 friend 帮助编写了脚本,但现在我遇到了另一个问题。
这是我现在的脚本:
var j = schedule.scheduleJob('*/5 * * * * *', function(){
var steamids = [];
con.query("SELECT * FROM counterStrikeGlobalOffensive", function (err, rows) {
for (var i = 0; i < rows.length; i++) {
steamids.push(rows[i].steam64ID);
}
//const steamIDs = ["2342342341234123", "23423412341234", "2342314123423"]; // Steam IDs to check
eachSeries(steamids, (steamID, callback) => {
CSGOCli.playerProfileRequest(CSGOCli.ToAccountID(steamID));
CSGOCli.on("playerProfile", function(profile) {
console.log(JSON.stringify(profile, null, 2));
callback();
});
}, (err) => {
// error thrown = set, else we're done
});
});
});
当我使用常量 steamID 时,它工作得很好,但是当我使用 steamids 时,它会给我一个错误。(我会解释)...
当我这样做时,console.log(steamids);它返回给我这个
[ '76561198152643711', '76561198213530057' ]
steamID 是
const steamIDs = ["2342342341234123", "23423412341234", "2342314123423"];
所以它几乎与常量 SteamID 相同,但常量在数字周围有“”,这不应该是它不起作用的原因,但也许我错了?
另外,我有回调(),但我怎样才能让它停止给我一个错误 错误:回调已被调用。
请询问任何其他信息:)
最佳答案
您会收到错误:回调已被调用。
,因为CSGOCli.on()
被执行了多次。因此它调用回调一次,然后事件再次触发。因此回调会再次被调用,但它应该只被调用一次。
有关简单的复制,请参阅此示例:
async.eachSeries([1, 2, 3], (data, callback) => {
console.log("Data:", data);
for(let i = 0; i < 2; i++) {
callback();
}
},
(err) => {
console.log("Callback: ", err);
});
但是,如果您在回调之前添加 return,如下所示:return callback();
,那么问题就会消失,因为函数将返回并且不会再次调用回调。
因此将您的代码更改为以下内容并查看它是否有效:
CSGOCli.on("playerProfile", function(profile) {
console.log(JSON.stringify(profile, null, 2));
return callback();
});
关于node.js - Nodejs async.eachSeries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46356379/