【问题标题】:nodejs:mysql not able to access global variable inside connection query callbacknodejs:mysql无法访问连接查询回调中的全局变量
【发布时间】:2017-12-10 13:45:29
【问题描述】:

下面是我的代码

我正在从客户端 javascript 向这个函数发送数据对象 数据打印它的值

socket.on 内

socket.on 内部 -> connection.query(sql,[], function(err,rows){data prints here})

socket.on 内部 -> connection.query(sql,[], function(err,rows){data prints here})-> connection.query(sql,[], function(err,rows){data prints未定义})

socket.on('sendmessage', function (data) {
    var data = JSON.parse(data);
    console.log(data);
    if (data.cuserid != '' && users_connected.hasOwnProperty(data.cuserid) && fs.existsSync(users_connected[data.cuserid]['sessionfile'])) {
        sql = "update hired set echat='Y' where eid=? and jobid=? and jid=? and status in ('given','finished')";
        connection.query(sql, [data.cuserid, data.jobid, data.userid], function (err, rows) {/*console.log(this.sql);*/ });
        sql = "insert into chat_messages(fromid,toid,jobid,message,received_date) select ?,?,?,?,NOW() from hired where echat='Y' and jchat='Y' and jobid=? and jid in(?,?) and eid in(?,?) and status in ('given','finished')";
        connection.query(sql, [data.cuserid, data.userid, data.jobid, data.message, data.jobid, data.cuserid, data.userid, data.cuserid, data.userid], function (err, rows) {
            console.log("dataretrieved : ", data); if (!err && users_connected[data.userid] != '') {
                socid = users_connected[data.userid]['socket'];
                sql = "select id as userid,username, userphoto from us_signup where id=?";
                connection.query(sql, [data.userid], function (err, rows) {
                    console.log(data);  //prints undefined                                   
                    if (!err) {
                        var data = {
                            'userphoto': rows[0].userphoto,
                            'username': rows[0].username,
                            'userid': rows[0].userid,
                            'cuserid': data.cuserid,
                            'message': data.message,
                            'jobid': data.jobid
                        }
                        io.to(socid).emit('message', JSON.stringify(data));
                    }
                })
            }
        });
    }

})

【问题讨论】:

  • 你明白那些connection.query() 调用是异步的吧?
  • @Pointy 是的兄弟
  • @Pointy 它在第一个 connection.query() 中打印数据值
  • 对,但关键是前两个查询将几乎同时启动。 connection.query() 函数调用立即返回,远在查询实际完成之前。
  • @Pointy 谢谢你提醒我异步调用

标签: javascript mysql node.js sockets


【解决方案1】:

您正在最深层次创建一个新作用域的data

if (!err) {
  var data = { ... }
  ...
}

这个变量在包含函数的顶部是hoisted,所以代码就相当于这个:

var data;          // clobbers the existing `data` variable...
console.log(data); // ...so here it will log `undefined`...                           
if (!err) {
  data = { ... }   // ...because it receives a value here.
  ...
}

解决方案:

  • 不要在那里使用var
  • 开始使用let

【讨论】:

  • 谢谢兄弟,我刚刚将数据更改为新数据
猜你喜欢
  • 2017-06-09
  • 1970-01-01
  • 2014-01-17
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 2019-03-29
  • 2018-03-21
相关资源
最近更新 更多