0

I currently have the following code.

  dbSetOrderTemplate(tx) {

    tx.executeSql('SELECT * FROM dr_template_relational WHERE template_id = (?)', [this.state.currentTemplateId],
    (trans, result) => {

      let currentOption;
      const resultValue = result.rows._array;
      let categories = [];

      resultValue.forEach(function (item, index) {
        if(currentOption != item.categories_id) {
          // looping through results to get each item in sqlite db
          tx.executeSql('SELECT * FROM dr_report_categories WHERE id = (?)', [item.categories_id],
          (transCat, catResult) => {
            categories.push(catResult.rows._array[0].name);
          });
          console.log(categories);
        }
        currentOption = item.categories_id;
      });
    },
    (err) => console.error(err)
    );

  }

The above code runs and selects an array of items from a local sqlite database. So in the first 'SELECT * FROM dr_template_relational WHERE template_id = (?)' I'm selecting an array and I pass to a const resultValue. Then on resultValue I do a forEach to loop through each item and then my goal is to push it to variable categories.

categories isn't being pushed into, in the currently declared variable. Obviously a problem with Scope but I can't figure out why.

I declare: let categories = []; then I run my foreach and then I execute another function called tx.executeSql. The tx.executeSql is a method inside of Expo for react native. I would expect categories.push to be pushing to the outside variable.

This works and gives me a console.log of the array pushed:

  (transCat, catResult) => {
    categories.push(catResult.rows._array[0].name);
    console.log(categories);
  }

but if I step out of tx.executeSql method:

  tx.executeSql('SELECT * FROM dr_report_categories WHERE id = (?)', [item.categories_id],
  (transCat, catResult) => {
    categories.push(catResult.rows._array[0].name);
  });
  console.log(categories);

This shows console.log Array [];

If categories isn't being read by the method then categories should cause an error undefined, no? I can't figure out what I'm doing wrong here. Please advice, thank you!

FabricioG
  • 3,107
  • 6
  • 35
  • 74
  • 1
    You are logging the array before it is populated. That happens later, when the callback is called. You need to embrace asynchrony here. – trincot Jun 17 '19 at 20:20
  • Thank you for the heads up. After digging more into it, it was more related to the foreach async nature. This answer helped: https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – FabricioG Jun 17 '19 at 21:41
  • 1
    The `forEach` is not async, it is the callback passed to `executeSql` that is. The answer you link to deals with an async function that is passed to `forEach`. That is not your case, but the effect is similar. With your code, things become async with the callback passed to `executeSql`. – trincot Jun 17 '19 at 21:44
  • Ah, makes perfect sense! Thank you! – FabricioG Jun 17 '19 at 21:44

0 Answers0