我正在使用节点创建Schedule Manager应用程序。JS+Express+Mongoose和我正在尝试搜索特定日期和地点的约会。如果未找到任何约会,我希望在开始日期和结束日期之间创建空白约会,以便在需要时准备就绪。
问题:
我是否可以以某种方式返回刚创建的约会对象,而不是调用
Appointments.find
再一次如果可能的话,最好的方法是什么?
示例:我设想创建一个新数组并通过迭代添加每个对象。
以下是我的模型的代码:
Appointment.find(query)
.exec()
.then((appointments) => {
if (appointments.length == 0) {
Location.findById(locationId)
.exec()
.then((location) => {
for (let j = location.available_time_start; j <= location.available_time_end; j += location.appointment_duration) {
var newAppointment = new Appointment();
newAppointment.start_date = new Date(day.getFullYear(), day.getMonth(), day.getDate(), j);
newAppointment.appointment_duration = location.appointment_duration;
newAppointment.location = location.id;
newAppointment.booked = false;
newAppointment.locked = false;
Appointment.createAppointment(newAppointment, function (err, appointment) {
if (err) throw err;
console.log(appointment.location + ' - ' + appointment.start_date);
});
}
// I WANT TO RETURN THE APPOINTMENTS HERE!
})
.catch((err) => {
console.log("Error while creating appointments: " + err);
});
} else {
// IT RETURNS AS EXPECTED WHEN PREVIOUSLY INCLUDED!
callback(null, appointments);
}
})
.catch((err) => {
console.log("Error while searching for appointments: " + err);
});