'process.nextTick(function() { throw err; })&#

2020-02-10 03:42发布

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

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

it immediately fails next with

process.nextTick(function() { throw err; }) ^TypeError: undefined is not a function at 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

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

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

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:

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

3条回答
ら.Afraid
2楼-- · 2020-02-10 04:26

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

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);
        });
    }
});
查看更多
在下西门庆
3楼-- · 2020-02-10 04:29

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 mongoose@4.0 --save in 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.

查看更多
Lonely孤独者°
4楼-- · 2020-02-10 04:36

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

查看更多
登录 后发表回答