Node.js – TypeError:Object#<Object>没有方法

我想包括一个模块,我发现,将有助于pipe理用户: 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:对象#没有方法'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; } }; 

       

网上收集的解决方案 "Node.js – TypeError:Object#<Object>没有方法"

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

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

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

尽pipesessionManagement声明被提升到模块的顶部(这就是为什么在将模块分配给module.exports时不会得到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