javascript - Node :mysql not able to access global variable inside connection query callback

标签 javascript mysql node.js sockets

下面是我的代码

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

在 socket.on 内部

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

在 socket.on 内部 -> connection.query(sql,[], function(err,rows){数据在这里打印})-> connection.query(sql,[], function(err,rows){数据打印未定义})

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));
                    }
                })
            }
        });
    }

})

最佳答案

您正在最深层次中创建一个新范围的数据:

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

关于javascript - Node :mysql not able to access global variable inside connection query callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44950808/

相关文章:

javascript - 如何从javascript中的字符串中获取唯一字符列表?

javascript - 如何更改 react 内置 Bootstrap 的原色?

javascript - 使用 Bootstrap 在 React 中显示行

mysql - Windows批处理脚本2种情况

javascript - for循环从redis延迟获取项目

javascript - 我在 Node.js Express 中遇到一些关于路由的问题

javascript - Django - 将 python 数据传递给 javascript

c# - 在 C# 中将对象、结构或数组插入 MySQL 表

mysql - 当某些记录有月/日/年而其他记录只有年份时如何存储日期?

javascript - 如何使电子邮件请求与动态 HTML 响应一起使用?