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

后端 未结 3 549
有刺的猬
有刺的猬 2020-12-16 15:50

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 bu

相关标签:
3条回答
  • 2020-12-16 16:13

    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.

    0 讨论(0)
  • 2020-12-16 16:23

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

    0 讨论(0)
  • 2020-12-16 16:31

    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);
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题