1

I am trying to include a module i found that will help manage users: http://www.codeproject.com/Articles/382561/Session-Management-in-Nodejs

Ive copied the code and put it in the same directory as my server.js

I require it by doing:

var express = require('express');
var http = require('http'), 
mysql = require("mysql");
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var sessionMgm = require("./sessionManagement");

Now in my socket i do this:

io.sockets.on('connection', function (socket) {

    socket.on('setUserInfo', function (data) {
        var sess = new Object();
        sess.sessionId = socket.id;
        sess.userId = data.userId;
        sess.username = data.username;
        sess.role = data.role;
        sessionMgm.add(sess);
    });


    socket.on("private", function(data) { 
        if(data.agentName.length <= 0) {
            data.agentName = 'Besökare';
        }       
        io.sockets.in('Room_' + data.user_id).emit('updatechat', data.agentName, data.msg);
        var user = sessionMgm.getSessionByUserId(data.id);

        console.log('::: A socket with ID ' + user + ' connected! ::: ');
    });

});

However i keep getting this error:

TypeError: Object # has no method 'getSessionByUserId'

Cant seem to figure out whats wrong, any ideas?

sessionManagement.js:

module.exports = sessionManagement;

var sessions = [];

//User roles list
var userRoles = {
  Admin: "administrator",
  User: "user",
  Supervisor: "supervisor"
};

var sessionManagement = {
  indexOf: function(sessionId) {
    for(var i in sessions) {
        if(sessions[i].sessionId == sessionId)
            return i;
    }

    return null;
  },
  indexOfUser: function(userId) {
    for(var i in sessions) {
        if(sessions[i].userId == userId)
            return i;
    }

    return null;
  },

  add: function(sessionData) {
    sessions.push(sessionData);
  },
  remove: function(sessionId) {
    var index = this.indexOf(sessionId);
    if(index != null) {
        sessions.splice(index, 1);
    } else {
        return null;
    }
  },
  removeByUserId: function(userId) {
    var index = this.indexOf(userId);
    if(index != null) {
        sessions.splice(index, 1);
    } else {
        return null;
    }
  },

  getSessionById: function(userId) {
    var index = this.indexOfUser(userId);
    if(index != null) {
        return sessions[index];
    } else {
        return null;
    }
  },
  getSessionByUserId: function(sessionId) {
    var index = this.indexOfUser(userId);
    if(index != null) {
        return sessions[index];
    } else {
        return null;
    }
  },

  isAdmin: function(userId) {
    var index = this.indexOfUser(userId);
    if(index != null) {
        if(users[index].role == userRoles.Admin) {
            return true;
        } else {
            return false;
        }
    } else {
        return null;
    }
  },
  getUsersByRole: function(role) {
    var usersByRole = [];
    for(var i in users) {
        if(users[i].role == role)
            usersByRole.push(users[i]);
    }

    return usersByRole;
  }
};

As madflow mentioned, you were missing module.exports = sessionManagement in sessionManagement.js

Then you got the error, because you were exporting sessionManagement, before initializing it. Moving the export line to the end of sessionManagement.js should fix that.

module.exports = sessionManagement; // <- you export here
    ...
    ...
    ...
var sessionManagement = { // and initialize here

Although sessionManagement declaration gets hoisted to the top of the module (and that's why you don't get Unexpected identifier or ReferenceError when assigning it to module.exports), it's initialization does not, so what really happens behind the scenes is something like that:

var sessionManagement;  // undefined at this point

module.exports = sessionManagement; // <- you export here, 
// but sessionManagement is undefined at this point 
// and so will be module.exports after this line
    ...
    ...
    ...
sessionManagement = { // and initialize here
4

1 回答 1

4

正如 madflow 提到的,你module.exports = sessionManagementsessionManagement.js

然后你得到了错误,因为你sessionManagement在初始化它之前正在导出。将出口线移到末尾sessionManagement.js应该可以解决这个问题。

module.exports = sessionManagement; // <- you export here
    ...
    ...
    ...
var sessionManagement = { // and initialize here

虽然sessionManagement声明被提升到模块的顶部(这就是为什么你没有得到Unexpected identifierReferenceError将它分配给时module.exports),但它的初始化没有,所以幕后真正发生的事情是这样的:

var sessionManagement;  // undefined at this point

module.exports = sessionManagement; // <- you export here, 
// but sessionManagement is undefined at this point 
// and so will be module.exports after this line
    ...
    ...
    ...
sessionManagement = { // and initialize here
于 2013-01-15T14:32:33.293 回答