0

Hello beautiful comunity!
I'm trying to write multiple fields in a table but I'm having synchrony problems, having the next output:

<!> T A B L E  |1|  C R E A T E D <!>
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);

<!> T A B L E  |2|  C R E A T E D <!>
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);

<!> T A B L E  |3|  C R E A T E D <!>
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);

When I should get the next output for every created table:

<!> T A B L E  |x|  C R E A T E D <!>
Executing (default): INSERT INTO `Programacion` (`id`,`dia`,`sucursal_id`,`torta_id`,`tamano_id`,`cantidad`,`estado`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);

The user submits a Json file that I have to transform into an array of 52 fields so that I can write it to my database, the resulting 52 objects are sent as follows:
tables.forEach((table) => {
        programacionServices.programacionCreate(table)
        .then(r  => {
            console.log(`\n<!> T A B L E  |${_id}|  C R E A T E D <!>`)
            _id = _id + 1;
        })
        .catch(e => reject(e))
})

programacionServices.programacionCreate:

class ProgramacionServices{
    programacionCreate(body){
        return new Promise((resolve, reject) => {
            Programacion.create(body)
            .then(r => resolve(r))
            .catch(e => reject(e));
        });
    }
}

I was trying with nested promises, using async/await inside of my functions, also I've used setTimeout, setInterval and clearInterval to test the point where it breaks but always the output in the console is the same, and just the first four fields are saved.

Thanks in advance!

EDIT

I've tryed with bluebird package but didn't work:

resolve(Promise.each(tables, (table) => {
    return programacionServices.programacionCreate(table)
        .then(r  => {
            console.log('inserted');
        })
        .catch(e => reject(e))
}).then(r => console.log('done')));
Arath
  • 5
  • 3

1 Answers1

0

If Programacion that you use as Programacion.create in your ProgramacionServices class is an instance of sequelize model, you don't need to wrap it with another promise. You don't need this:

programacionCreate(body){
    return new Promise((resolve, reject) => {
        Programacion.create(body)
        .then(r => resolve(r))
        .catch(e => reject(e));
    });
}

Remove the promise around the .create and make it async function.

async programacionCreate(body){
    Programacion.create(body);
}

Then for your tables array you could write something like this:

for (const table of tables) {
    await programacionServices.programacionCreate(table);

    console.log(`\n<!> T A B L E  |${_id}|  C R E A T E D <!>`);

    _id = _id + 1;
}
Mansur
  • 1,622
  • 14
  • 27
  • It works well! Thanks u Mansur!! Only one more thing that I would like to know... can u explain me why with async/await it works but not with promises? Thanks again! – Arath Jul 16 '20 at 13:19
  • That's probably because when you foreach your tables you almost immediately call all programacionCreate functions at once (we can think it like this). So console.log runs multiple times before _id = _id + 1 – Mansur Jul 21 '20 at 19:19
  • OK, I get it! Thanks u :) – Arath Jul 24 '20 at 15:59
  • You can read here for details. https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – Mansur Jul 24 '20 at 21:59