'process.nextTick(function() { throw err; })' - Undefined 不是函数 (mongodb/mongoose) [英] 'process.nextTick(function() { throw err; })' - Undefined is not a function (mongodb/mongoose)

查看:19
本文介绍了'process.nextTick(function() { throw err; })' - Undefined 不是函数 (mongodb/mongoose)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to connect to my mongodb using 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

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

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

它立即失败了

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

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

这里是 showCollections:

And here goes 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

我通过键入 mongod --dbpath folder 运行 mongodb,它成功地等待端口 27017 上的连接".

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

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

Also, my node_modules from package.json (npm)

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

非常感谢您的帮助...

Thank you very much for your help...

堆栈跟踪:

> 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 实例时也遇到了这些问题:

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

{ [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...

推荐答案

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

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

他们用 listCollections 替换了它.您应该获得相同的效果:

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

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

这篇关于'process.nextTick(function() { throw err; })' - Undefined 不是函数 (mongodb/mongoose)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆