title: How to make HBS await object from server before rendering page?
body: <p>I am trying to display some data from server in HBS but it doesn't work every time. Some times HBS will act as if there is no data being passed so I'm guessing it loads too fast before the object is rendered, how can I await for the req.user to reach front end before loading /profile?</p>
  <p>The code below grabs req.user object from dbs and renders it to hbs:</p>
  <pre><code>router.get(&quot;/profile&quot;, authController.isLoggedIn, (req, res) =&gt; {
  if (req.user) {
  res.render(&quot;profile&quot;, { user: req.user});
  }
  </code></pre>
  <p>HBS:</p>
  <pre><code>&lt;div class=&quot;order-info-content&quot;&gt;
        {{#unless user.orders.[0].id}}
          &lt;h4&gt;You do not have any recent orders&lt;/h4&gt;
          &lt;br&gt;
          &lt;a href=&quot;&quot; id=&quot;account-info-edit&quot;&gt;View our latest competitions!&lt;/a&gt;
          &lt;br&gt;
        {{/unless}}
        
        {{#if user.orders.[0].id}}
        &lt;div class=&quot;order-text&quot;&gt;
          &lt;h4&gt;{{user.orders.[0].date}}&lt;/h4&gt;
          &lt;h4&gt;ID: #{{user.orders.[0].id}}&lt;/h4&gt;
          &lt;h4&gt;Total: £{{user.orders.[0].total}}&lt;/h4&gt;
          &lt;/div&gt;
  
          &lt;div class=&quot;order-images&quot;&gt;
          &lt;img id=&quot;order-image&quot; src=&quot;/Images/price 2.jpg&quot; alt=&quot;&quot;&gt;
          &lt;/div&gt;
        {{/if}}
  </code></pre>
  <p>Edit:
  Back-end:</p>
  <pre><code>exports.isLoggedIn = async (req, res, next) =&gt; {
  if (req.cookies.jwt) {
  try {
    const decoded = await promisify(jwt.verify)(
      req.cookies.jwt,
      process.env.JWT_SECRET
    );
  
    db.query(
      &quot;SELECT * FROM user WHERE id = ?&quot;,
      [decoded.id],
      (error, result) =&gt; {
  
        if (!result) {
          return next();
        }
        email = result[0].email;
        req.user = result[0];
        db.query(&quot;SELECT * FROM orders WHERE email = ?&quot;, [email],
        (error, result) =&gt; {
          
          if (result[0]) {
            
            req.user[&quot;orders&quot;] = result;
            console.log(req.user.orders)
  
          };
        }
        )
        return next();
      }
    );
  } catch (error) {
    return next();
  }
  } else {
  next();
  }
  };
  </code></pre>
  <p>This code will work 50% of the time, output will either be &quot;You do not have any orders&quot; OR the orders will show, the output is random between theses two option when I reload the page.</p>