0

How 2 use Async Await on multiple queries and query under .map() function in NodeJS and MySQL

NOTE:: First we will execute the first query using the Async and await and than get 5 Ids from the first query and than using the first query array we will use the .map() function and pass the Id one by one into the inner query using async and await and create the object for every loop and save these object onto and array.

Please give me some code for this functionality.

1 Answer 1

1

Here it is a pseudocode from your description which demonstrates how can be waited for multiple async operation to complete:

async function getRecords() {
  const recordIds = await execQuery() // get ids from db

  const promises = [];

  recordIds.map((e) => {
    promises.push(execQuery())
  });

  await Promise.all(promises);
}

From your description it is unclear why you need separate queries to retrieve objects. Can't you use (in operator)[https://www.w3schools.com/mysql/mysql_in.asp] in following query:

SELECT * FROM table WHERE id IN(1, 4, 8)

Sign up to request clarification or add additional context in comments.

2 Comments

User save data 3 times a day (12PM, 3PM, 6PM). So If we get the data through IN condition than all users data in one array. Due to this there are some issue to display data in <td> at react end. Suppose User add the data at 3PM and skip the 12PM data, than 3PM data show in 12PM <td> ` userlist.map((userdata, index) => tasklist.map((taskdata, indextask) => if (props.tasklist.time_seconds >= 41400000 && props.tasklist.time_seconds <= 45000000) { return ( <td style={{ padding: '10px', textAlign: 'left' }}>{nl2br(props.tasklist.tasklist)}</td> ) }else { return ( "" ) } ) ) `
suggest you to process the list in frontend. Such queries are complex and can decrease scalability.

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.