Node.js - 类型错误 : Object #<Object> has no method

标签 node.js

我正在尝试包含一个我发现可以帮助管理用户的模块: 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

我需要这样做:

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 # has no method '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;
  }
};

最佳答案

正如 madflow 提到的,您在 sessionManagement.js 中缺少 module.exports = sessionManagement

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

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

尽管 sessionManagement 声明被提升到模块的顶部(这就是为什么在分配时不会得到 UnexpectedidentifierReferenceError它到 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

关于Node.js - 类型错误 : Object #<Object> has no method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14339654/

相关文章:

mysql - Node 没有为成功的存储过程返回值

node.js - 使用对话模型对 AMAZON.NUMBER 类型进行 Alexa 输入验证

javascript - V8 分析 : linux-tick-processor not working with d8

node.js - geoNear 的 MongoDB 性能问题

angularjs - npm 启动我的 Angular2 项目时出现重复标识符导出错误

node.js - 检查空值快速验证器

node.js - 从 NodeJS 服务器下载多个文件

node.js - 在 OpenShift 上运行 Gulp 构建

javascript - 如何跨平台设置环境变量?

node.js - 如何在nodejs Angular Universal Web应用程序上强制域为https?