The following is my code:
以下是我的代码:
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);
The dbCallback
function is never called. Any help would be greatly appreciated!
永远不会调用dbCallback函数。任何帮助将不胜感激!
2 个解决方案
#1
10
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.
在数据库连接打开后,您是否尝试过执行查询?我没有Mongoose服务器来测试它,但这是我的第一个猜测。
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.
对不起,如果这不是最终修复它。
#2
6
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.
确保您实际连接到Mongo实例,否则查询将被挂起,并且通常不会抛出或返回错误。我的Mongo URI中的错误导致了这一点。
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.
在您的代码中,您将Mongo URI设置为mongodb:// localhost / mydatabase。我想这可能不正确,是你问题的原因。将您的URI更改为localhost:27017,这是Mongo设置为运行的默认端口。
#1
10
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.
在数据库连接打开后,您是否尝试过执行查询?我没有Mongoose服务器来测试它,但这是我的第一个猜测。
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.
对不起,如果这不是最终修复它。
#2
6
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.
确保您实际连接到Mongo实例,否则查询将被挂起,并且通常不会抛出或返回错误。我的Mongo URI中的错误导致了这一点。
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.
在您的代码中,您将Mongo URI设置为mongodb:// localhost / mydatabase。我想这可能不正确,是你问题的原因。将您的URI更改为localhost:27017,这是Mongo设置为运行的默认端口。