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;
  }
};
4
  • 2
    Did you put module.exports = sessionManagement; in your sessionManagement.js file? Even better: Post the code Commented Jan 15, 2013 at 14:21
  • Dident have that in there, now i get a different error: TypeError: Cannot call method 'getSessionByUserId' of undefined .. so something happend :) Commented Jan 15, 2013 at 14:29
  • 2
    @Elvin post your sessionManagement.js, otherwise we can only guess Commented Jan 15, 2013 at 14:30
  • @Elvin Put: module.exports = sessionManagement; at the bottom. Commented Jan 15, 2013 at 14:32

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

That worked! :) Ofc now i get new errors :/ var index = this.indexOfUser(userId); ^ ReferenceError: userId is not defined
@Elvin change sessionId to userId in getSessionByUserId definition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.