0

I'm trying to pass arrEntree in my return, however it's inside a promise. How would I fix this issue? The function query iniaites a new promise but like everytime ArrEntree is printed empty, because it gets called before the promise is finished processing. How would I finish the promise then only call my return...? How would I solve this issue.

function query(statement, params, first) {
  return new Promise(function(resolve, reject) {
    connection.query(statement, (params || []), function(err, rows) {
      if (err) {
        console.log("err", err);
        if (err.code === "ETIMEDOUT") {
          connectToDb();
          return query(statement, params, first);
        }
        reject(err);
      } else {
        if (first) {
          resolve(rows[0]);
        } else {
          resolve(rows);
        }
      }
    });
  });
}





function sendPlanEmails(){
      if (plans.length === 0) {
        return true;
      }

      const orders = plans.map(plan=>{
        const arrEntree = []
        const planData = srcData.plans[plan.productId];
        const entrees = plan.entrees;
        const entreeID = Object.keys(plan.entrees);
        query('select * from entrees where entree_id in (' + entreeID.join(',') + ')')
          .then(function(results){
            results.forEach(function(element){
              entreeID.forEach(function(second_elem){
                if(element.entree_id==second_elem){
                  console.log('worked until here')
                  const quantity = plan.entrees[second_elem];
                  const name = element.entree;
                  arrEntree.push({name:name, quantity:quantity});
                }
              })

            })

          })

        return {
          days:plan.days.days,
          planDataTitle:planData.title,
          breakfast:plan.meals[0],
          lunch:plan.meals[1],
          dinner:plan.meals[2],
          entreeArry:arrEntree
        }; 
      });
      const template_mp = srcData.orderPage.mailchimp_template_mp || 'order_confirmation';
      return utils.sendMealPlanEmail(template_mp, {
          from: from,
          order: orders,
        });
    }
Biplov
  • 1,136
  • 1
  • 20
  • 44
  • looks like you don't know how asynchronous code works - https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Jaromanda X Sep 01 '17 at 06:54
  • it also looks like you don't know how if you return from a function, code after the return doesn't get executed ... you have such a problem in `plans.map(plan=>{` – Jaromanda X Sep 01 '17 at 06:55
  • you also have a problem in `query` function, `return query(statement, params, first);` will mean if you do have a `err.code === "ETIMEDOUT"` then the promise returned by the initial call to `query` will never be fulfilled or rejected - this problem is easy to fix `if (err.code === "ETIMEDOUT") { connectToDb(); resolve(query(statement, params, first)); } else { reject(err); }` – Jaromanda X Sep 01 '17 at 06:57
  • In my case query actually returns me something though. I get the data I need from arrEntree. I need to be able to pass that data into my return call for orders. I understand I did return query but even if I get rid of the word return it doesn't seem to be working. – Biplov Sep 01 '17 at 06:59
  • see [this fiddle](https://jsfiddle.net/Lwxszgut/) which addresses the issues, I believe the real issue is that you have a misplaced `});` :p - although, you need to understand that the `order` property in the argument to `sendMealPlanEmail` will be an array of **Promises** - possibly fixed in https://jsfiddle.net/Lwxszgut/1/ – Jaromanda X Sep 01 '17 at 07:02
  • It bothers me why you have 2 return statements `1st return was: return query;` and `2nd return was return {object}`; – masterpreenz Sep 01 '17 at 07:09
  • @JaromandaX Thank you so much for the help, however it says TypeError: orders.then is not a function – Biplov Sep 01 '17 at 07:14
  • sorry, misplaced `)` ,,, https://jsfiddle.net/jaromanda/Lwxszgut/3/ – Jaromanda X Sep 01 '17 at 08:47
  • you'll probably want to return Promise.all too - https://jsfiddle.net/jaromanda/Lwxszgut/4/ – Jaromanda X Sep 01 '17 at 08:52

0 Answers0