繁体   English   中英

无法在NodeJS MySQL中使用全局变量

[英]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_matchesnull (因为这是while循环停止的条件)。

connection.query异步的

请参见为什么在函数内部修改变量后变量却没有改变? -异步代码参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM