javascript - how to catch errors when rendering ejs view Node.js -


please have notice return records database contains missing data view renders error page show in image, code looks correct, if there example missing id in data records reason. undefined. should expected. problem how avoid , catch such errors or possibly render decent view message. have server 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')}); }); 

and in ejs template

  <% if(data.length){                         for(var in data){ %>                 <div class="article-detail">                     <div class="product-detail">                         <img  alt="no image" src="/images/<%= data[i].itemimage %>">                         <div class="description"><h5 style="color: #0044cc"> <%= data[i].itemname %></h5>                             <span><b><%=__('item number:')%></b><%= data[i].itemnumber.touppercase() %> </span>                             <span ><b><%=__('borrowed date:')%></b></span>                             <span class="head2"><%= data[i].borrowdate.todatestring() %> </span><br>                             <span><b><%=__('offer type:')%></b><%= data[i].offertype%> </span>                             <span><b><%=__('booking number:')%> </b><%= data[i].bookingno.touppercase() %> </span><br>                             <span class="head1"><b><%=__('return date:')%></b></span><span class="head2" style="color:crimson;"><%= data[i].returndate.todatestring() %> </span>                             <span><b><%=__('offer status:')%></b><%= data[i].itemstatus %> </span><br>                             <span class="spl-chr"><%=__('offered to:')%><a href="/user/<%= typeof data[i].userid!='undefined' ? data[i].userid._id:data[i].guestuserid._id %> %>"><%= typeof data[i].userid!='undefined' ? data[i].userid.username:data[i].guestuserid.firstname %></a></span>                             <span class="manage-btn">                                                                  <span><a class="btn btn-default" href="/manage/edit/<%= data[i].id %>"><%=__('edit')%></a></span>                                 <span><a class="btn btn-danger" href="/manage/delete/<%= data[i].id %>"><%=__('delete')%></a></span>                             </span>                         </div>                     </div>                 </div>                 <% } 

enter image description here

to report errors in node, use errorhandler

var errorhandler = require('errorhandler'); 

at top and

if( env == 'development') {     errorhandler.title = "ups...";     app.use(errorhandler()); } 

just before app.listen(), show formatted error page useful during development.

if want catch errors in production , display alternative (user visible) view, should use express middleware architecture. can define custom error class , use pass info view failed error handler function:

// define custom error class function mycustomerror(custominfo) {     error.capturestacktrace(this, this.constructor);     this.name = this.constructor.name;     this.message = "error:" + custominfo; } util.inherits(mycustomerror, error);  // view function view1(req,res,next) {    try {      //... prepare context      return res.render('template.ejs', context);    }    catch(e)    {      // here can analyze cause of error , pass useful info      // error handler      return next(new mycustomerror('template failed:' + e.message));    } }  function handleerror: function(err,req,res,next) {   // it's how handle error, can render   // alternate view or redirect error view   if( err instanceof mycustomerror )      return res.redirect(301, 'error.html');     next(err); }  // of set // ... app.get('/view1',view1); app.use(handleerror); // ... 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -