0

My changes are all in this function:

fetchRows(sqlQuery) {
    let aPromise = new Promise((resolve, reject) => {
      setTimeout(function () {
        if (sqlQuery) {
          resolve(I.getResults(sqlQuery));
        }
        reject("Not a valid query");
      }, 38);
    });

    return aPromise.then((sqlQuery) => {
      console.log('Success:', sqlQuery);
    }).catch((errorMessage) => {
      console.log(errorMessage);
    });
  },

What property is undefined? Is something missing from the promise structure? (I am a js promise noob) If i remove everything and just wrap up the "I.getResults(sqlQuery)" it runs fine, something about the promise is throwing it off I think.

Here is the getResults function

  /**
   * Get query resultset
   * @param {*} sqlQuery
   */
  getResults(sqlQuery) {
    return connection.then(client => {
      return client.query({
        rowMode: 'array',
        text: sqlQuery,
      }).then(res => {
        return res.rows;
      }).catch(e => {
        client.release();
        pool.end();
        console.error('query error', e.message, e.stack);
      });
    });
  }
user2344015
  • 23
  • 2
  • 7

2 Answers2

1

Your problem is that resolve () doesn't break the flow, so you also call reject.

To fix this, use return resolve (...) so that you jump out of the function scope and don't trigger the reject, that in turn leads you to return undefined.

thomasmichaelwallace
  • 6,136
  • 1
  • 20
  • 29
  • That sounds good let me implement this, gosh of course I think I had an else here too initially that I maybe lost. – user2344015 Oct 23 '18 at 21:06
  • 1
    i don't think so because even if the reject method is being invoked it will fire the .catch and that will never cause the undefined issue. – Mohamed Assem Oct 23 '18 at 21:08
  • 1
    The property zero suggests that something outside the code shared is expecting an array to return; my guess is that not returning the result because of the caught reject means you're returning undefined and undefined[0] would cause that error messages. – thomasmichaelwallace Oct 23 '18 at 21:17
1

you can try to refactor the new Promise function to be async function and instead of setTime out which leads you to resolve with undefined data because getResults is asynchronous function as well so you can await on the calling of getResults and resolve with your results so you edit can be like the following:

const fetchRows = (sqlQuery) => {
 const aPromise = new Promise(async (resolve, reject) => {
  if (sqlQuery) {
    const sqlQueryResult = await I.getResults(sqlQuery);
    return resolve(sqlQueryResult);
  }
  return reject('Not a valid query');
 });
 return aPromise.then((sqlQuery) => {
  console.log('Success:', sqlQuery);
 }).catch((errorMessage) => {
  console.log(errorMessage);
 });
};
  • I don't see how this make any difference. In any case, if `I.getResults` returns a promise, why is that wrapped in another promise? – Jaime Oct 23 '18 at 21:32
  • you can try the following code to see the issue with the getResults function even if it returns Promise you don't know when it will resolve so you have to await on the calling of getResults and after that resolve fetchRows Promise with the result for more explanation implement getResults as following and try to resolve it inside fetchRows it will result undefined : const getResults = () => { setTimeout(() => new Promise(resolve => resolve('aaaaaa')), 10); }; – Mohamed Assem Oct 23 '18 at 21:49
  • That doesn't sound correct @Mohamed. In your example implementation of `getResults` you are not returning, so `undefined` is returned by default. Even if your intention was to use the implicit return feature of arrow functions, your promise is wrapped with a `setTimeout` call so you would be returning the result of that call, again not a promise. – Jaime Oct 23 '18 at 22:21
  • This is a very known antipattern: https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – Jaime Oct 23 '18 at 22:23