M
M
mayday692020-08-19 12:16:17
MySQL
mayday69, 2020-08-19 12:16:17

How to make one mysql query wait for another in Node js?

The posids array should store the IDs of the newly created rows in the MYSQL table and
then transfer them to another table, but it turns out that both queries are processed first
, and only then this array is filled, and the column remains empty. Please tell me how to do it better

while (i < rowcount) {
        pool.query(`INSERT INTO positions(len,count,sqr,cost) VALUES(?,?,?,?)`, [request.body['inputlen' + rowids[i]], 
                request.body['inputcount' + rowids[i]], request.body['sqrr' + rowids[i]], request.body['cost' + rowids[i]]
        ], function(error, results, fields) {
            posids.push(results.insertId);
            console.log(posids);
        })
        i++;
    }

    if (wave && color && thickness && fthickness && date) {
        console.log('first' + posids);
        pool.query(`INSERT INTO orders(wavetype,thick,fthick,color,createdate,readydate,positions) VALUES(?,?,?,?,?,?,?)`, 
               [wave, thickness, fthickness, color, currentdate, date, posids.toString()], function(error, results, fields) {
            console.log(rowcount, rowids);
            response.redirect('/home');
            response.end();
        })
    } else {
        response.send('Введены не все данные');
        response.end();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-sem, 2020-08-19
@mayday69

determine the sequence of actions through promises
https://learn.javascript.ru/promise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question