'process.nextTick(function() { throw err; })' - 未定义不是函数 (mongodb/mongoose)

'process.nextTick(function() { throw err; })' - Undefined is not a function (mongodb/mongoose)

我正在尝试使用 nodejs 和 socket.io 连接到我的 mongodb。我能够连接到数据库,因为我在我的控制台中得到 'connection accepted' 但在 nodejs 端,一旦我 - 确实 - 得到

Connection to mongodb://localhost:27017 established through mongoose

接下来立即失败

process.nextTick(function() { throw err; }) ^TypeError: undefined is not a function at 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 folder 运行 mongodb 并成功 'awaits connections on port 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

我尝试按照此处的其他问题修复它们,但也没有任何效果...

你不应该指定完整路径并有一个导出模块吗?
...类似于:

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

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

根据提供的信息,您似乎使用的是 mongodb 2.0 驱动程序。 db.collectionNames 方法被删除。查看此页面的 "Db Object" 部分 - 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);
        });
    }
});

对我来说,当我不得不强制关闭我的电脑时,它开始出现这个错误,因为它变得没有响应。下次我启动我的快速服务器时,会弹出此错误。我在终端中通过 运行 npm install mongoose@4.0 --save 解决了它。 可能是当您的服务器或数据库被强制关闭并且某些代码未保存在幕后的 mongod 中时弹出此错误。