title: how to catch errors when rendering ejs view Node.js
body: <p>Please I have notice that anything i return records from a database which contains some missing data my view renders with an error page like show in the image, everything about my code looks correct, but if there is for example a missing ID in the data records for any reason. I get undefined. Which should be expected. But my problem is how to I avoid this and catch such errors or possibly render a decent view with a message.
  I have this as my server code</p>
  
  <pre><code>ItemOffers.paginate({'OfferedByID': req.user._id}, {
      page: page,
      limit: 10,
      populate: ['UserID', 'GuestUserID']
  }, function (err, result, pageCount) {
      if (err) {
          console.log('Error Occured' + err)
      }
      console.log(result);
      mypageCount = pageCount;
      res.render('manageoffers', {data: result, mylink: data, PCount: pageCount ,SuccessMessage: req.flash('SuccessMessage'), ErrorMessage: req.flash('ValidationError')});
  });
  </code></pre>
  
  <p>and this in my ejs template </p>
  
  <pre><code>  &lt;% if(data.length){
                          for(var i in data){ %&gt;
                  &lt;div class="article-detail"&gt;
                      &lt;div class="product-detail"&gt;
                          &lt;img  alt="No image" src="/images/&lt;%= data[i].ItemImage %&gt;"&gt;
                          &lt;div class="description"&gt;&lt;h5 style="color: #0044cc"&gt; &lt;%= data[i].ItemName %&gt;&lt;/h5&gt;
                              &lt;span&gt;&lt;b&gt;&lt;%=__('Item Number:')%&gt;&lt;/b&gt;&lt;%= data[i].ItemNumber.toUpperCase() %&gt; &lt;/span&gt;
                              &lt;span &gt;&lt;b&gt;&lt;%=__('Borrowed Date:')%&gt;&lt;/b&gt;&lt;/span&gt;
                              &lt;span class="head2"&gt;&lt;%= data[i].BorrowDate.toDateString() %&gt; &lt;/span&gt;&lt;br&gt;
                              &lt;span&gt;&lt;b&gt;&lt;%=__('Offer Type:')%&gt;&lt;/b&gt;&lt;%= data[i].OfferType%&gt; &lt;/span&gt;
                              &lt;span&gt;&lt;b&gt;&lt;%=__('Booking Number:')%&gt; &lt;/b&gt;&lt;%= data[i].BookingNo.toUpperCase() %&gt; &lt;/span&gt;&lt;br&gt;
                              &lt;span class="head1"&gt;&lt;b&gt;&lt;%=__('Return Date:')%&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class="head2" style="color:crimson;"&gt;&lt;%= data[i].ReturnDate.toDateString() %&gt; &lt;/span&gt;
                              &lt;span&gt;&lt;b&gt;&lt;%=__('Offer Status:')%&gt;&lt;/b&gt;&lt;%= data[i].ItemStatus %&gt; &lt;/span&gt;&lt;br&gt;
                              &lt;span class="spl-chr"&gt;&lt;%=__('Offered to:')%&gt;&lt;a href="/user/&lt;%= typeof data[i].UserID!='undefined' ? data[i].UserID._id:data[i].GuestUserID._id %&gt; %&gt;"&gt;&lt;%= typeof data[i].UserID!='undefined' ? data[i].UserID.UserName:data[i].GuestUserID.FirstName %&gt;&lt;/a&gt;&lt;/span&gt;
                              &lt;span class="manage-btn"&gt;                                 
                                  &lt;span&gt;&lt;a class="btn btn-default" href="/manage/edit/&lt;%= data[i].id %&gt;"&gt;&lt;%=__('Edit')%&gt;&lt;/a&gt;&lt;/span&gt;
                                  &lt;span&gt;&lt;a class="btn btn-danger" href="/manage/delete/&lt;%= data[i].id %&gt;"&gt;&lt;%=__('Delete')%&gt;&lt;/a&gt;&lt;/span&gt;
                              &lt;/span&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;% }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KcPhQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KcPhQ.jpg" alt="enter image description here"></a></p>