title: Node.js - Passing multiple objects to same EJS file
body: <p>I have multiple MySQL query results where I render the first and make callable the rest ones to a single EJS file (/data) as objects.</p>
  
  <p>I render only the <em>first</em> query results (<strong>obj</strong>) and then I make the rest callable to the EJS (<strong>rsum_total_sales</strong> &amp; <strong>rsum_count_active</strong>) </p>
  
  <p>It seems that the first time that I load the EJS file I get frontend 500 errors like </p>
  
  <pre><code>   rsum_total_sales is not defined
  </code></pre>
  
  <p>but when I reload the browser I get the EJS to loading fine, showing all the needed results that got from the route file.</p>
  
  <p>Weird thing that even the first time, in console.log() I see that all three query results have been put in the objects I made, however they cannot be yet callable to the EJS except the first ones which renders directly to it.</p>
  
  <p>From the EJS error I can see that it passes the first connection.query results (obj) which is rendered but not the rest two (rsum_total_sales &amp; sum_count_active) which I just make their objects callable until I reload the browser.</p>
  
  <p><strong>route file</strong></p>
  
  <pre><code>var obj = {};
  var qsum_total_sales = 'SELECT SUM (total_price) AS s_total_price FROM myrecords'; 
  var qsum_count_active = 'SELECT COUNT (*) AS s_count_active FROM myrecords WHERE NOT status = "canceled" ';
  var qdata = 'SELECT * FROM myrecords';
  
  router.get('/data', function(req, res){
      connection.query(qdata, function(err, result) {
          if(err){
              throw err;
             } else {
                  obj = {print: result};
                  res.render('data', obj);
                  console.log(obj);
          }
      });
  
     connection.query(qsum_total_sales, function(err, rows, result) {
         if(err){
             throw err;
         } else {
             rsum_total_sales = JSON.parse(rows[0].s_total_price).toFixed(2);
             console.log(rsum_total_sales); 
             //First time prints result but it can't be called to EJS until reloading EJS
         }
     });
  
    connection.query(qsum_count_active, function(err, rows, result) {
         if(err){
             throw err;
         } else {
             rsum_count_active = JSON.parse(rows[0].s_count_active);
             console.log(rsum_count_active);  
             //First time prints result but it can't be called to EJS until reloading EJS
         }
     }); 
  });
  </code></pre>
  
  <p><strong>EJS file</strong></p>
  
  <pre><code>&lt;tbody&gt;
      &lt;% print.forEach(function (datatable) { %&gt;
          &lt;tr&gt;
               &lt;td&gt;&lt;%= datatable.id %&gt;&lt;/td
               &lt;td&gt;&lt;%= datatable.full_name %&gt;&lt;/td&gt;
               //rest of loop
           &lt;/tr&gt;                              
          &lt;% }) %&gt;
  &lt;/tbody&gt;
      // some html code
  &lt;h4&gt;Total sales sum is: &lt;%- rsum_total_sales %&gt;&lt;/h4&gt;
  &lt;h4&gt;Total active records are: &lt;%- rsum_count_active %&gt;&lt;/h4&gt;
  </code></pre>
  
  <p><strong>Frontend Error</strong> (only when it is the 1st time that EJS loads to the browser)</p>
  
  <pre><code>ReferenceError: C:\NodeJS\CRUD-1\views\data.ejs:39
      37|         &lt;/table&gt;
      38|         &lt;hr/&gt;
  &gt;&gt;  39|         &lt;h4&gt;Total sales sum is: &lt;%- rsum_total_sales %&gt;&lt;/h4&gt;
      40|         &lt;h4&gt;Total active records are: &lt;%- rsum_count_active %&gt;&lt;/h4&gt;
  
  rsum_total_sales is not defined
  at eval (eval at &lt;anonymous&gt; (C:\NodeJS\CRUD-1\node_modules\ejs\lib\ejs.js:481:12),
  &lt;anonymous&gt;:47:17)
  at returnedFn (C:\NodeJS\CRUD-1\node_modules\ejs\lib\ejs.js:512:17)
  at View.exports.renderFile [as engine] (C:\NodeJS\CRUD- 1\node_modules\ejs\lib\ejs.js:364:31)
  at View.render (C:\NodeJS\CRUD-1\node_modules\express\lib\view.js:126:8)
  at tryRender (C:\NodeJS\CRUD-1\node_modules\express\lib\application.js:639:10)
  at EventEmitter.render (C:\NodeJS\CRUD-1\node_modules\express\lib\application.js:591:3)
  at ServerResponse.render (C:\NodeJS\CRUD-1\node_modules\express\lib\response.js:960:7)
  at Query._callback (C:\NodeJS\CRUD-1\routes\data.js:36:17)
  at Query.Sequence.end (C:\NodeJS\CRUD-1\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
  at Query._handleFinalResultPacket (C:\NodeJS\CRUD-1\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
  </code></pre>