title: Pass Data Value from MongoDB to ejs Template in Node.js
body: <p>I have an express app where I'm using ejs for rendering the views. For example, this is one of the views I have:</p>
  
  <pre><code>&lt;a href="/admin/categories" class="list-group-item"&gt;
                              &lt;span style="margin-right: 6px" class="glyphicon glyphicon-folder-close"&gt;&lt;/span&gt;
                              Categories
                              &lt;span class="badge categoriesCount"&gt;&lt;%= catCount %&gt;&lt;/span&gt; &lt;!-- get data from mongo --&gt;
                          &lt;/a&gt;
  </code></pre>
  
  <p>and in my routes file, I have gotten the values from mongodb and tried passing them into the view like say:</p>
  
  <pre><code>router.get('/', (req, res) =&gt; {
  
  let cat_count = 0,
      prod_count = 0,
      user_count = 0,
      order_count = 0;
  
  Category.count({}, (err, count) =&gt; {
      if (!err) {
          cat_count = count;
          console.log('Cat count from db:\t' + count);
      } else {
          console.error('Error Fetching Categories Count:\t' + err);
      }
  });
  
  Products.count({}, (err, count) =&gt; {
      if (!err) {
          prod_count = count;
          console.log('Prod count from db:\t' + count);
      } else {
          console.error('Error Fetching Products Count:\t' + err);
      }
  });
  
  Users.count({}, (err, count) =&gt; {
      if (!err) {
          user_count = count;
          console.log('User count from db:\t' + count);
      } else {
          console.error('Error Fetching Users Count:\t' + err);
      }
  });
  
  Orders.count({}, (err, count) =&gt; {
      if (!err) {
          order_count = count;
          console.log('Orders count from db:\t' + count);
      } else {
          console.error('Error Fetching Orders Count:\t' + err);
      }
  });
  
  res.render('index', {
      catCount: cat_count,
      prodCount: prod_count,
      userCount: user_count,
      orderCount: order_count
    });
  });
  </code></pre>
  
  <p>where the <code>catCount</code> is an actual argument for the variable in my template. This does not work and I'm stuck with figuring this way out.</p>
  
  <p>I have also tried using the <code>DOM querySelector('className').innerHTML</code> but that also doesn't work.</p>
  
  <p>Which is the best way to send values to my template, I would prefer ejs.
  Thanks.</p>