【发布时间】:2017-04-10 21:42:13
【问题描述】:
在我的应用程序中,我像这样使用带有蓝鸟的猫鼬:
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
在同一个文件中,我拥有一个在数据库中搜索“约会”-documents 的函数。该函数的返回应该是找到的文档的结果数组,而不是承诺、查询或其他东西。
但不幸的是,程序似乎会在承诺实现之前跳回调用函数。那我该如何处理呢?
这是我当前状态下的函数:
get_intersecting_appointment: function(appointment) {
var appointmentQuery = AppointmentModel.find({
$and: [{
begin: {
$lte: appointment.begin
}
},
{
end: {
$gte: appointment.end
}
}
]
});
return appointmentQuery.then(function(appointments) {
debug("Found appointments:");
debug(appointments);
if (appointments) {
return appointments;
} else {
return [];
}
});
},
提示:调用函数在内部debug("Found appointments:") 之前打印“下一个调试输出”;从上面。可以说,调用函数如下所示:
appointment_is_available: function(appointment) {
var appointments = dbcon.get_intersecting_appointment(appointment);
//...
}
【问题讨论】:
标签: node.js mongodb mongoose bluebird