Tuesday, 1 October 2019

**newbie here** Execute 2 sql queries together in Express

Hi,I'm new to node and hope somebody can help me.I'm trying to return some Json from 2 SQL queries and one forEach loop. But I'm lost in the scope or in the call back hell :)app.get('/exercises/by_day/:day', (req, res, next ) => { const day = parseInt(req.params.day); let data = []; pool.query('SELECT id, title, img_url FROM exercise WHERE day = $1 ORDER BY title', [day], (error, results) => { if (error) { next(error); } results.rows.forEach(row => { // query check second table if the timestamp was today pool.query('SELECT count(id) as cnt FROM set WHERE set.exercise_id = $1 AND created_at >= now()::date', [row.id], (error, results) => { if (error) { next(error); } row.training_today = (results.rows[0].cnt > 0); data.push(row); }); }); data = results.rows; }); res.status(200).json(data); });

Submitted October 01, 2019 at 07:59PM by 23hurra

No comments:

Post a Comment