【问题标题】:Cannot use global variable in NodeJS MySQL无法在 NodeJS MySQL 中使用全局变量
【发布时间】:2014-12-02 08:33:10
【问题描述】:

我无法访问 MySQL 查询回调中的即将到来的_matches 数组,这是怎么回事?

我在 console.log(upcoming_matches[1]); 上收到“TypeError: Cannot read property '1' of null”错误消息表示由于某种原因正在重置变量的行(猜测,我对 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]);
    });
}

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    因为在执行传递给connection.query 的回调的那一刻,while 循环已经完成并且upcoming_matchesnull(因为这是while 循环停止的条件)。

    connection.query异步

    Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 2017-05-20
      • 2018-06-09
      • 2020-01-02
      • 2011-11-03
      • 2012-10-09
      • 1970-01-01
      相关资源
      最近更新 更多