node.js 'process.nextTick(function() { throw err; })' - Undefined 不是一个函数 (mongodb/mongoose)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29449190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:37:04  来源:igfitidea点击:

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

node.jsmongodbmongoosesocket.io

提问by Fane

I'm trying to connect to my mongodbusing nodejs and socket.io. I am able to connect to the database because I get 'connection accepted' in my console but on the nodejs side, as soon as I - indeed - get

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

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

通过 mongoose 建立到 mongodb://localhost:27017 的连接

it immediately fails next with

接下来立即失败

process.nextTick(function() { throw err; }) ^TypeError: undefined is not a function at showCollections**

process.nextTick(function() { throw err; }) ^TypeError: undefined 不是 showCollections 中的函数**

And here goes 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);
      });
    }
  });

}

And here is the content of my database folder:

这是我的数据库文件夹的内容:

_tmp (empty folder)
local.0
local.ns
mongod.lock

I run the mongodb by typing mongod --dbpath folderand it successfully 'awaits connections on port 27017'.

我通过键入mongod --dbpath 文件夹运行 mongodb ,它成功地“等待端口 27017 上的连接”。

Also, my node_modules from package.json(npm)

另外,我的 node_modules 来自package.json(npm)

"dependencies": {
    "express": "^4.9.6",
    "socket.io": "latest",
    "mongodb": "~2.0",
    "mongoose": "*"
  }

Thank you very much for your help...

非常感谢您的帮助...

StackTrace:

堆栈跟踪:

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

EDIT:

编辑:

I'm as well having these problems when trying to run the nodejs instance:

在尝试运行 nodejs 实例时,我也遇到了这些问题:

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version

I tried fixing them as other questions here would tell but nothing worked either...

我试着像这里的其他问题一样修复它们,但也没有任何效果......

回答by Gene

From the provided information, it looks like you are using mongodb 2.0 driver. The db.collectionNames method was dropped. Check out the "Db Object" section of this page - https://github.com/mongodb/node-mongodb-native/blob/0642f18fd85037522acf2e7560148a8bc5429a8a/docs/content/tutorials/changes-from-1.0.md#L38

从提供的信息来看,您似乎使用的是 mongodb 2.0 驱动程序。db.collectionNames 方法被删除。查看此页面的“数据库对象”部分 - https://github.com/mongodb/node-mongodb-native/blob/0642f18fd85037522acf2e7560148a8bc5429a8a/docs/content/tutorials/changes-from-138.md#L

They've replaced it with listCollections. You should get the same effect with:

他们用 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);
        });
    }
});

回答by Fane

Shouldn't you specify the full path and have an export module?
...something like:

你不应该指定完整路径并有一个导出模块吗?
...就像是:

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

If I'm wrong it's because I don't fancy moongodb :)

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

回答by aakata

For me this error started popping up when i had to force shut down my pc as it became unresponsive. Next time i started my express server this error popped up. I solved it by running npm install [email protected] --savein the terminal. May be this error pops up when your server or database is force closed and some code is not save in mongod behind the scenes.

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