【发布时间】:2015-06-09 13:07:57
【问题描述】:
我正在尝试使用 nodejs 和 socket.io 连接到我的 mongodb。我能够连接到数据库,因为我在控制台中获得了“连接已接受”,但在 nodejs 端,只要我 - 确实 - 得到 p>
通过 mongoose 建立到 mongodb://localhost:27017 的连接
接下来会立即失败
process.nextTick(function() { throw err; }) ^TypeError: undefined is 不是 showCollections 中的函数**
这里是 showCollections:
var showCollections = function(db, callback) {
mongoose.connection.db.collectionNames(function(error, names) {
if (error) {
throw new Error(error);
} else {
console.log("=>Listening mongo collections:");
names.map(function(cname) {
mongoose.connection.db.dropCollection(cname.name);
console.log("--»"+cname.name);
});
}
});
}
这是我的数据库文件夹的内容:
_tmp (empty folder)
local.0
local.ns
mongod.lock
我通过键入 mongod --dbpath 文件夹 运行 mongodb,它成功地“等待端口 27017 上的连接”。
另外,我的 node_modules 来自 package.json (npm)
"dependencies": {
"express": "^4.9.6",
"socket.io": "latest",
"mongodb": "~2.0",
"mongoose": "*"
}
非常感谢您的帮助...
堆栈跟踪:
> TypeError: undefined is not a function
> at showCollections (/usr/share/nginx/www/index.js:77:25)
> at NativeConnection.callback (/usr/share/nginx/www/index.js:46:3)
> at NativeConnection.g (events.js:199:16)
> at NativeConnection.emit (events.js:104:17)
> at open (/usr/share/nginx/www/node_modules/mongoose/lib/connection.js:485:10)
> at NativeConnection.Connection.onOpen (/usr/share/nginx/www/node_modules/mongoose/lib/connection.js:494:5)
> at /usr/share/nginx/www/node_modules/mongoose/lib/connection.js:453:10
> at /usr/share/nginx/www/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:5
> at /usr/share/nginx/www/node_modules/mongoose/node_modules/mongodb/lib/db.js:200:5
> at connectHandler (/usr/share/nginx/www/node_modules/mongoose/node_modules/mongodb/lib/server.js:272:7)
编辑:
我在尝试运行 nodejs 实例时也遇到了这些问题:
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
我尝试修复它们,因为这里的其他问题会告诉你,但也没有任何效果......
【问题讨论】:
-
你能发布打印出来的实际堆栈跟踪吗?这应该会告诉您 哪里 在
showCollections()中错误源自。 -
@mscdex 如何打印堆栈跟踪?它应该默认打印吗?
-
是的,它应该已经显示在你得到的 TypeError 下。
-
@mscdex 我更新了问题
-
showCollections()中的第 75 行是哪一行?
标签: node.js mongodb mongoose socket.io