不能在NodeJS MySQL中使用全局variables
我无法访问MySQL查询callback里面的discovered_matches数组,怎么回事?
我遇到了一个'TypeError:无法读取属性'null'错误消息在console.log(coming_matches [1]); 线,这表明该variables正在重置出于某种原因(猜测,我不是很有经验的Javascript)?
var regex = regex code.. while ((upcoming_matches = regex.exec(body)) != null) { HLTVID = upcoming_matches[1].split("-"); connection.query('SELECT * FROM `csgo_matches` WHERE HLTVID=' + HLTVID[0], function(err, rows, fields) { if (err) throw err; console.log(upcoming_matches[1]); }); }
因为此时传递给connection.query
的callback被执行,所以while
循环已经完成,而upcoming_matches
为null
(因为这是while
循环停止的条件)。
connection.query
是asynchronous的 。
在函数内部修改后,为什么我的variables没有改变? – asynchronous代码引用