I have some mysql Query's in NodeJs that depends to a above result. Here is my code:
const sms = async function() {
await connection.query('select * from sms where sms.status_id = 2',
(err, rows) => {
if (err) throw err
rows.forEach(row => {
startdate = moment(row.statdate).format('YYYY-MM-DD HH:mm:ss');
enddate = moment(row.enddate).format('YYYY-MM-DD HH:mm:ss');
connection.query('select * from positions where sms_id = "'+row.id+'" and verified is null order by date asc',
(err, rows2) => {
if (err) throw err
rows2.forEach(row2 => {
connection.query('HERE IS A QUERY TO INSERT A REGISTER IN ANOTHER TABLE', data, (err,res) => {
if (err) throw err
console.log(`Inserted ${res.insertId}`);
})
})
}
);
})
}
)
The problem is, that way, I don't have the expected result in the middle query.
How can i do this synchronous?
I try to use Promises, but i don't understand how to do.