Javascript 在Mongoose上手动创建对象列表

Javascript 在Mongoose上手动创建对象列表,javascript,express,mongoose,model,Javascript,Express,Mongoose,Model,我正在使用node.JS+Express+Mongoose创建一个ScheduleManager应用程序,并尝试搜索特定日期和地点的约会。如果找不到约会,我希望在开始日期和结束日期之间创建空白约会,以便在需要时准备就绪 问题: 我是否可以以某种方式返回刚创建的约会对象,而不是调用约会。再次查找?如果可能的话,最好的方法是什么 示例:我想象创建一个新数组并通过迭代添加每个对象 以下是我的模型的代码: Appointment.find(query) .exec() .then((ap

我正在使用node.JS+Express+Mongoose创建一个ScheduleManager应用程序,并尝试搜索特定日期和地点的约会。如果找不到约会,我希望在开始日期和结束日期之间创建空白约会,以便在需要时准备就绪

问题:

我是否可以以某种方式返回刚创建的约会对象,而不是调用
约会。再次查找
?如果可能的话,最好的方法是什么

示例:我想象创建一个新数组并通过迭代添加每个对象

以下是我的模型的代码:

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);
    });
Appointment.find(查询)
.exec()
。然后((约会)=>{
如果(appointments.length==0){
Location.findById(locationId)
.exec()
。然后((位置)=>{
对于(设j=location.available\u time\u start;j{
log(“创建约会时出错:+err”);
});
}否则{
//当以前包含时,它会按预期返回!
回调(空,预约);
}
})
.catch((错误)=>{
log(“搜索约会时出错:+err”);
});

我通过使用数组解决了这个问题,并在保存每个约会后通过
回调
函数返回它:

                let newAppointments = Array();

                for (let j = location.available_time_start; j <= location.available_time_end; j += location.appointment_duration) {
                    let 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;

                    newAppointments.push(newAppointment);
                    newAppointment.save()
                    .then((appointment) => {
                        console.log(appointment.location + ' - ' + appointment.start_date);
                    })
                    .catch((err) => {
                        console.log("Error while creating appointments: " + err);
                    })
                }

                callback(null, newAppointments);
让newappoints=Array();
对于(设j=location.available\u time\u start;j{
console.log(appointment.location+'-'+appointment.start_date);
})
.catch((错误)=>{
log(“创建约会时出错:+err”);
})
}
回调(空,新约会);