Nodejs async.eachSeries

几个月前,我问了几个问题,最近我又回到了这个脚本。 我想出了一些东西,一个朋友帮助了这个剧本,但现在我又遇到了另外一个问题。

这是我现在的脚本:

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 }); }); }); 

当我使用不变的蒸汽发生器时,它可以很好地工作,但是当我使用蒸汽发生器时,它给了我一个错误(我将解释)…

当我这样做,console.log(steamids); 它回报我这个

 [ '76561198152643711', '76561198213530057' ] 

和steamIDs是

 const steamIDs = ["2342342341234123", "23423412341234", "2342314123423"]; 

所以它几乎与恒定的SteamID一样,但不断有“”的数字,这不应该是为什么它不工作,但也许我错了?

此外,我有callback(),但我怎么能让它停止给我一个错误错误:callback已被调用。

请求任何其他信息,请:)

你得到Error: Callback was already called. 因为CSGOCli.on()被执行多次。 所以它会调用callback一次,然后事件再次触发。 所以callback被再次调用,但它只应该被调用一次。

为了简单的再现,请看这个例子:

 async.eachSeries([1, 2, 3], (data, callback) => { console.log("Data:", data); for(let i = 0; i < 2; i++) { callback(); } }, (err) => { console.log("Callback: ", err); }); 

但是,如果你在callback之前添加return,像这样: return callback(); ,那么问题就会消失,因为函数将会返回,并且不会再次调用callback函数。

所以改变你的代码,看看它是否工作:

 CSGOCli.on("playerProfile", function(profile) { console.log(JSON.stringify(profile, null, 2)); return callback(); });