Node.js - TypeError:Object#<object>没有方法</object>

时间:2013-01-15 14:16:46

标签: node.js

我正在尝试包含一个我发现有助于管理用户的模块: http://www.codeproject.com/Articles/382561/Session-Management-in-Nodejs

  

我已复制代码并将其放在与server.js相同的目录中

我要求:

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

现在在我的套接字中我这样做:

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! ::: ');
    });

});

但是我一直收到这个错误:

  

TypeError:Object#没有方法'getSessionByUserId'

似乎无法弄清楚什么是错的,有什么想法吗?

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

1 个答案:

答案 0 :(得分:4)

正如madflow所说,你在module.exports = sessionManagement

中遗失了sessionManagement.js

然后您收到错误,因为您在初始化之前导出了sessionManagement。将导出行移动到sessionManagement.js的末尾应该可以解决这个问题。

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

虽然sessionManagement声明被提升到模块的顶部(这就是为什么在将Unexpected identifier分配给ReferenceError时没有得到module.exportsvar 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 )的原因初始化没有,所以幕后真的发生了类似的事情:

{{1}}