Mongoose挂在find()查询

时间:2018-01-26 00:13:18

标签: database mongodb express mongoose keystonejs

我正在为网站开发后端。主应用程序使用KeystoneJS为创建内容的用户提供管理UI,Keystone在MongoDB(特别是mongoose)之上运行。内容提供给主网站和移动应用程序,因此为这些提供后端API服务器我正在编写一个单独的Express应用程序。

后端服务器尝试使用mongoose访问基础Mongo数据库。我使用以下连接字符串和选项调用mongoose.connect()

var db_connection = 'mongodb://db_user:db_pass@localhost:27017/db';
var db_options = { 
    autoReconnect: false, 
    bufferMaxEntries: 0,
    poolSize: 10,
};

连接已成功打开,由mongoose.connection.on('connected', ...);记录,我可以使用MongoDB shell手动查看数据库,以确认数据库中是否有文档。

但是,当我尝试使用:

查询数据库时
var events = Event.find();
events.exec((err, events) => {
    console.log('/api/events: events found, sending to client');
    if (err) res.send(err);
    res.json(events);
}).catch(err => console.log('error: ' + err));

其中Eventkeystone.list('Event').model,查询挂起,exec回调永远不会被调用,因为控制台日志永远不会打印到终端。

我已经正确初始化了Keystone并导入了模型,因此我不认为问题出在Keystone模型上,而是使用了mongoose本身。另外,如果我在Keystone应用程序中执行上述代码,它会成功运行,而不是在我的后端Express应用程序中。

0 个答案:

没有答案