简体   繁体   中英

Node js / javascript promises not waiting to finish the sequelize query

I am expecting the below promise to return customer and blox slot also as part of booking record in second.then(). But addCustomer and addBooking has not executed or yet to be executed.

when I added await in addBooking or addCustomer, it didnt work.

I am not sure where i am missing

const bookingCreated = await Booking.create(data).then((booking) => {
    const customers = data.customer_id;
    if (data.customer_id !== '') {
        customers.forEach((customer) => booking.addCustomer(customer, booking.id));
    }
    tempBoxSlots.forEach((slot) => boxSlotBooking.addBooking(slot, booking.id));
    return booking;
}).then((result) => {
    console.log('result');
    console.log(result.id);
    const boxSlotAttributes = ['id', 'start_time', 'duration'];
    const retBooking = Booking.findOne({
        where: {
            id: result.id
        },
        include: [{
                model: BookingType
            },
            {
                model: BookingSource
            },
            {
                model: Venue
            },
            {
                model: Customer,
                as: 'lead'
            },
            {
                model: BoxSlot,
                attributes: boxSlotAttributes,
                through: {
                    attributes: []
                }
            },
            {
                model: Customer,
                attributes: ['id', 'firstname', 'lastname', 'email'],
                through: {
                    attributes: []
                }
            }

        ]
    });

    return retBooking;
}).catch((err) => {
    console.log(err);
});
console.log('bookingCreated');
console.log(bookingCreated);

According to the examples given in this sequalize documentation , the methods that are automatically added when you define associations, like add* , return promises.

So instead of the forEach loops with addCustomer and addBooking , you would need to collect those promises (with .map ) and pass those to Promise.all :

await Promise.all([...customers.map((customer) => booking.addCustomer(customer, booking.id)),
                   ...tempBoxSlots.map((slot) => boxSlotBooking.addBooking(slot, booking.id))]);

I would also replace all the then calls you have, with await . Go for one pattern: either then chaining, or async/await . But for readability, avoid the mix.

Another remark: the check if (customer_id == '') is a bit odd, because customer_id is an array, and so checking for an non-empty array is better done with if (customer_id.length) . Moreover, there is no reason to exclude an empty array from executing what is in that if block, so you can actually do without that if condition all together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM