我正在尝试使用nodejs和socket.io 连接到我的 mongodb 。我能够连接到数据库,因为一旦我-确实- 获得了我在控制台中但在Node.js端上的“连接接受”
通过猫鼬建立与mongodb:// localhost:27017的连接
接下来它立即失败
process.nextTick(function(){throw err;})^ TypeError:undefined不是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上的连接”。
另外,我来自 package.json (npm)的node_modules
"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
我尝试修复它们,因为这里的其他问题会告诉我,但是也没有任何效果…
根据提供的信息,您似乎正在使用mongodb 2.0驱动程序。db.collectionNames方法已删除。查看此页面的“ Db对象”部分-https: //github.com/mongodb/node-mongodb- native/blob/0642f18fd85037522acf2e7560148a8bc5429a8a/docs/content/tutorials/changes- from-1.0.md#L38
他们已将其替换为listCollections。您应该获得与以下相同的效果:
mongoose.connection.db.listCollections().toArray(function(err, names) { if (err) { console.log(err); } else { names.forEach(function(e,i,a) { mongoose.connection.db.dropCollection(e.name); console.log("--->>", e.name); }); } });