本文介绍了执行Mongoose查询后,从不调用回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码:

mongoose.connect('mongodb://localhost/mydatabase');
  var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log('DB connection opened');
});
// ...
var dbCallback = function(err, body) {
  // ...
};
// ...
var StuffModel = mongoose.model('Stuff', StuffSchema);
StuffModel.find({}).exec(dbCallback);

永远不会调用 dbCallback 函数。任何帮助将不胜感激!

The dbCallback function is never called. Any help would be greatly appreciated!

推荐答案

在数据库连接打开后,您是否尝试过查询 ?我没有Mongoose服务器来测试它,但这是我的第一个猜测。

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.

这篇关于执行Mongoose查询后,从不调用回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:58