Callback function never called after Mongoose query is executed

前端 未结 3 753
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 14:25

The following is my code:

mongoose.connect(\'mongodb://localhost/mydatabase\');
  var db = mongoose.connection;
db.on(\'error\', console.error.bind(console, \'co         


        
相关标签:
3条回答
  • 2021-02-14 14:53

    Have you tried doing your query after the database connection opens? I don't have a Mongoose server to test it, but that would be my first guess.

    var Stuff = mongoose.model('Stuff', StuffSchema);
    
    db.once('open', function () {
      Stuff.find({}, function (e, body) {
        console.log('It worked!');
      });
    });
    

    Sorry, if this doesn't end up fixing it.

    0 讨论(0)
  • 2021-02-14 14:54

    Make sure that you are actually connected to your Mongo instance, otherwise queries will be left hanging and often no error is thrown or returned. An error in my Mongo URI was causing this for me.

    In your code, you have your Mongo URI set to mongodb://localhost/mydatabase. I imagine that this is probably not correct and is the cause of your problem. Change your URI to just localhost:27017, which is the default port that Mongo is set to run on.

    0 讨论(0)
  • 2021-02-14 15:06

    I faced similar issue.

    I was calling process.exit(0) after queries, so the app exited before mongoose have a chance to run callback.

    So i removed process.exit(0) and it worked!

    0 讨论(0)
提交回复
热议问题