'process.nextTick(function(){throw err;})' – Undefined不是函数(mongodb / mongoose)

我正在尝试使用nodejs和socket.io连接到我的mongodb 。 我能够连接到数据库,因为我的控制台中接受了“连接接受”,但在nodejs一侧,只要我确实得到

连接到mongodb:// localhost:27017通过mongoosebuild立

它立即失败了

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

你不应该指定完整的path,并有一个导出模块?
…就像是:

 mongoose.connection.db.collectionNames(function (err, names) { console.log(names); // [{ name: 'dbname.myCollection' }] module.exports.Collection = names; } 

如果我错了,那是因为我不喜欢moongodb 🙂