简体   繁体   中英

Anonymous function and closures in socket.io

I' have this code, know that require anonymus closure function, but don't understand how it works. If I run it there is a TypeError: undefined is not a function . Can some one explain me anonymus closure functions with the help of this code?

mysql= require('mysql');
var connection = mysql.createConnection({});

function check_auth(input, callback){
 var sql = "query to mysql";
connection.query(sql, function(err, results) {
    if (err) callback(err);
    if (results.length  > 0) {
        callback(null,results.values); //this is the line with error
    }else{
        callback(null, false);
    } 
  });
  };
  var io = require('socket.io').listen(5678);
  io.configure(function () {
  io.set('authorization', function(req, callback) {
    check_auth(req.query.s, function(err, result) {
        if (err) {
            return console.log('error:(');

        }
        if(result === false) {
            return callback('notauth', false);
        } else {
            return callback(null, result);;
        }
      });
    });
});

You code looks good, but you have an error in your code: missing ); };

mysql= require('mysql');
var connection = mysql.createConnection({});

function check_auth(input, callback){
    var sql = "query to mysql";
    connection.query(sql, function(err, results) {
        if (err) callback(err);
        if (results.length  > 0) {
            callback(null,results.values); //this is the line with error
        }else{
            callback(null, false);
        } 
    }); // missing );
}; // missing };

io.configure(function () {
io.set('authorization', function(req, callback) {
check_auth(req.query.s, function(err, result) {
        if (err) {
            return console.log('error:(');

        }
        if(result === false) {
            return callback('notauth', false);
        } else {
            return callback(null, result);;
        }
      });
    });
});

There seems to be scoping issue in your code. You can't really call a function from another scope without referencing that scope. if you do:

io.configure(function () {
  io.set('authorization', function(req, callback) {
    var check_auth = function(...) {};   // <=== local defined

    // then you can call this way
    check_auth(...);
  }
}

Since your check_auth() is defined outside, the callback of io.set() has its own scope, it doesn't know anything about check_auth(). So you have to point to the scope that has check_auth() defined. Something like this:

var me = this;          // <==== scope that has check_auth defined
io.configure(function () {
  io.set('authorization', function(req, callback) {

    // then you can call this way
    me.check_auth(...);
  }
}

Or you can do closure approach by assigning check_auth to a variable and call it inside the callback. Something like this:

var check_auth = function(...) {};
io.configure(function () {
  io.set('authorization', function(req, callback) {

    // then you can call this way
    check_auth(...);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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