0

I am looping through an array of selected index comparing each value to a database of machine pricing, and returning the price of each selected index. the problem is, the result repData1 return individual results, I want those resuls to displayed in an array for I can manipulate the array. I have tried push, concat.... string results is displayed for each item rather than a whole.

for (let a = 0; a < selectedindex.length; a++) {
  wixData
    .query('MachinePricing')
    .contains('title', selectedindex[a])
    .find()
    .then(async (results) => {
      if (results.items.length > 0) {
        let repData = results.items;
        let repData1 = repData.map(({ prices }) => prices);
        console.log(repData1);
      }
    });
}
0

2 Answers 2

1

Don't loop async calls using iterators

Instead do this

const a = 0
const repData = [];

function getData = () => {
  if (a >= selectedindex) {
    processRepData();
    return;
  }
  wixData
    .query('MachinePricing')
    .contains('title', selectedindex[a])
    .find()
    .then(results => {
      if (results.items.length > 0) {
        repData.concat(results.items.map(({prices}) => prices));
      }
      a++;
      getData()
    });
}    
getData()
Sign up to request clarification or add additional context in comments.

Comments

1

I think what you are doing is this (run a query for each selected index and extract the returned prices into an array):

const queries = selectedindex.map(ix =>  wixData
    .query('MachinePricing')
    .contains('title', ix)
    .find())
const results = await Promise.all(queries)
const prices = results.flatMap(r => r.items.map(i => i.prices))

3 Comments

Interestingly it worked! 100% thanks!
This is a more elegant solution using await that my simpler version
This is a more elegant solution using await than my simpler version that does not use await at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.