title: Undefined push to array - Basic Application using ExpressJs and MySQL
body: <p>First of all, I have to tell you I'm pretty noob in this &quot;universe&quot;. I'm using: ExpressJs, MySql, Body-Parser, Express-session, Ejs template for creating an Basic Contacts Application in Node.</p>
  <p>My database is composed from 3 tables:</p>
  <ul>
  <li>user (user_id, first, second name, username, password)</li>
  <li>contacts (ct_id, first, second name, phone numb.)</li>
  <li>user_contacts (user_id, ct_id) --&gt; foreign keys for user and contacts</li>
  </ul>
  <p>I want to listing on  /myProfile page all details about user and his contacts.
  I don't know how to handle the select queries.</p>
  <p>So, after some documentation I did this:</p>
  <pre><code>conn.query('SELECT * FROM user_contacts WHERE user_id= ?', req.session.user_id, function (err, result) { 
          if(err) throw err;
          console.log(result);
          var queryArray = &quot;&quot;;
          for(var i = 0; i &lt; result.length; i++){
              queryArray += `SELECT * FROM contacts WHERE ct_id= ${result[i].ct_id}; `;
          }
          console.log(queryArray);
           conn.query(queryArray, function (err, result) { 
               if(err) throw err;
               console.log(result);
               res.render('myProfile/contacts', {
                  title: `${req.session.user_nickname}'s Contacts`,
                  data:   result
              });
            }); 
       });
  </code></pre>
  <p>But I have an error</p>
  <blockquote>
  <p>ER_PARSE_ERROR: You have an error in your SQL syntax;</p>
  </blockquote>
  <p>..when queryArray.length &gt; 1</p>
  <p>I searched and it's something about Multiple statement queries but I dont know how to solve it.</p>
  <h1>Edit 2:</h1>
  <p>I modify my code..</p>
  <pre><code>conn.query('SELECT * FROM user_contacts WHERE user_id= ?', req.session.user_id, function (err, result) { 
          if(err) throw err;
          var datas = [];
          for(var i = 0; i &lt; result.length; i++){
              getContacts    =   function(query){
                  conn.query(query, function (err, result) { 
                      console.log('Creating data');
                      data = {
                          user:       req.session.user_nickname,
                          contact:{
                              ct_firstName:   result[0].ct_firstName,
                              ct_SecondName:  result[0].ct_SecondName,
                              ct_PhoneNumber: result[0].ct_PhoneNumber
                          }   
                      }
                      return data;
                   });
              }
              console.log('Send data to array');
              datas.push(getContacts(`SELECT * FROM contacts WHERE ct_id = ${result[i].ct_id}`));
          }
          console.log(datas); // [ undefined, undefined ] 
          res.render('myProfile/contacts',{
              title:  `${req.session.user_nickname}'s profile`,
              data:   datas
          })
       });
  </code></pre>
  <p>But now my array contain undefined objects?? Any solution?
  Maybe is something about scope?</p>
  <p>My result:</p>
  <ul>
  <li>Send data to array</li>
  <li>Send data to array</li>
  <li>[ undefined, undefined ]</li>
  <li>Creating data</li>
  <li>Creating data</li>
  </ul>
  <p>I push the object to array before creating it. How is it possible?</p>