代码之家  ›  专栏  ›  技术社区  ›  Adalberto Ferreira

在Mongoose上手动创建对象列表

  •  0
  • Adalberto Ferreira  · 技术社区  · 6 年前

    我正在使用节点创建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);
        });
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Adalberto Ferreira    6 年前

    我通过使用数组并通过 callback 保存每个约会后的功能:

                    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);