title: Node JS mysql looping query push to array
body: <p>I'm having some trouble with below code. Can someone please take a look and advise what's going on?</p>
  <pre><code>const name = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]
  
    let query1 = ('SELECT name FROM table WHERE name = ? AND status IN (&quot;Pending&quot;,&quot;Active&quot;) limit 1')
    if (name instanceof Array) {
  
      async function getNames() {
  
        try {
          name.forEach(async (name) =&gt; {
            let newName = []
            let rows = await promisePool.query(query1, name);
  
            if (rows[0].length &gt; 0) {
              if (rows[0])
                newName.push(rows[0][0].uid_name)
                console.log(newName)
            }
            return (rows);
  
          })
        }
        catch (err) {
          console.log('ERROR =&gt; ' + err);
          return err;
        }
      }
  
      await getNames();
  
  console.log(newName) returns (if row is found) 
  ['a']
  ['b']
  </code></pre>
  <p>how can I push any returning value to the array ? <code>desired output ['a','b']</code> so I can compare it to the original array.</p>
  <p>Thanks in advance,
  Ucin</p>
  <p>I have changed the code a bit but to be honest I'm still not that of an expert.</p>
  <pre><code>const name = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]
  let query1 = ('SELECT name FROM table WHERE name = ? AND status IN (&quot;Pending&quot;,&quot;Active&quot;) limit 1')
   
    if (name instanceof Array) {
  
      async function getNames() {
        
        try {
         for(names of name) {
           
            let rows = await promisePool.query(query1,[uid,challenge_type,names]);
    
          
              return (rows);
            
          }
          }
       catch (err) {
        console.log('ERROR =&gt; ' + err);
        return err;
    }
      }
    
    let newVar = await getNames();
      console.log(newVar[0])
  </code></pre>
  <p>the newVar only returns 1 line, where there should be 2 names.</p>
  <pre><code>name.forEach(async (element) =&gt; {
           
            let rows = await promisePool.query(query1,[uid,challenge_type,element]);
    
            if (rows[0].length &gt; 0) {
              if (rows[0])
             /*  newName.push(rows[0][0].uid_name) */
              console.log(rows[0])
              
                 
        return (rows);
  </code></pre>
  <p>if I go with <code>forEach...Of</code>, I can console.log it within scope, but return(rows) is undefined</p>