[英]Cannot use global variable in NodeJS MySQL
我无法访问MySQL查询回调中的即将出现的数组,为什么呢?
我在console.log(upcoming_matches [1])上遇到了'TypeError:Cannot read property'1'of null'错误消息。 这行表明该变量由于某种原因而被重置(猜测,我对Java语言不是很了解)?
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
的回调的那一刻, while
循环已经完成,并且upcoming_matches
为null
(因为这是while
循环停止的条件)。
connection.query
是异步的 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.