Why await promise doesn't return all items

Viewed 56

I am using an await on promise mechanism with nodejs to get a list of rows from the database filtering on dates with between. I don't get any results, in other words the xlsTz array doesn't get filled. I get an empty array but I am pretty sure that there are elements to be returned after performing SQL query directly.

The thing that is more confusing, is that if i write exact dateStart and dateEnd given in my database, I get an array with only one item which I presume is last given element in the list I wished to get.

app.get('/getpackages/:dateStart/:dateEnd/:limit', async (req, res) => {
  var xlsTz = []

  const promiseQuery = (sql, dateStart, dateEnd, limit) => {
    return new Promise((resolve, rej)=> {
      db.query(sql, [dateStart,dateEnd,limit], function (err, results) {
        if (err) rej(err);
        resolve(results)
      });
    })
  }

  const sql = 'SELECT * FROM collection WHERE XlFile != "" AND Start BETWEEN ? AND ? LIMIT ?';

  const dateStart = req.params.dateStart;
  const dateEnd = req.params.dateStart;
  const limit = Number(req.params.limit);
  
  const results = await promiseQuery(sql, dateStart, dateEnd, limit)
  for (const counter in results) {  
    
    xlsTz.push(results[counter].XlsSourceFile);
  }

  console.log(xlsTz)

  res.send('Posts fetched...');

});

Can anyone help? Any ideas would be helpful

1 Answers

it looks like you are using res twice one on the router and another inside promiseQuery

Related