Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4038

nodejs:mysql not able to access global variable inside connection query callback

Below is my code

I am sending data object from client side javascript to this function data prints its values

inside socket.on

and

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

and

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

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

})

Upvotes: 1

Views: 1650

Answers (1)

robertklep
robertklep

Reputation: 203534

You are creating a newly scoped data in the deepest level:

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

This variable is hoisted to the top of the containing function, so the code becomes the equivalent of this:

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.
  ...
}

Solutions:

  • don't use var there
  • start using let

Upvotes: 2

Related Questions