Ben Crabbe Ben Crabbe - 3 months ago 29
Node.js Question

Node Express ejs Error: Failed to lookup view "error" in views directory

I am making an express app with ejs and mongoose.

I am getting this error:

Error: Failed to lookup view "error" in views directory "/Users/ben/Documents/csMSc/web/site/app/views"
at EventEmitter.app.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/application.js:555:17)
at ServerResponse.res.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:938:7)
at module.exports (/Users/ben/Documents/csMSc/web/site/app/app.js:94:7)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:321:12)
at IncomingMessage.next (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:261:10)
at fn (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:933:25)
at EventEmitter.app.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/application.js:557:14)
at ServerResponse.res.render (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:938:7)
at app.use.res.render.message (/Users/ben/Documents/csMSc/web/site/app/app.js:83:9)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSsc/web/site/app/node_modules/express/lib/router/index.js:321:12)


from two calls to
res.render()
where the data that is passed in comes back from a mongoose query e.g.:

if(req.query.author !== undefined) {
var author = req.query.author;
Post.find().where('author').equals(author).sort({ created: -1 }).limit(10).exec(function(err, authorsPosts) {
if (err) return res.send("error");
if(authorsPosts.length==0) {
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts,
error: "Sorry there are no posts with that tag."
});
} else {
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts
});
}
});
}


and the other one is the same but with a query of

Post.find( { tags : { $elemMatch: { $in : tagList } } } ).limit(10).exec(function(err, taggedPosts) {


However all my other render calls on this view are working just fine including one later in the same function:

//or just latest
Post.find().sort({ created: 1 }).limit(10).exec(function(err, latestPosts) {
if (err) return res.send(err);
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: latestPosts
});
});


where Im pretty sure latestPosts is in exactly the same format as authorsPosts in the one above.

There are no calls to render a view called error.

The error data passed to some of the calls to
res.render('pages/index')
above is passed using a custom filter

//custom ejs filter, sets to default value if data not supplied
ejs.filters.get = function(obj, prop, def) {
return obj[prop] === undefined ? def : obj[prop];
};


which appears in the file app/views/pages/index.ejs as

<p><%=: locals | get:'error','' %> </p>


My ejs setup looks like:

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs').renderFile);


heres the whole offending route function in all its horrible glory

router.get('/', function(req, res, next) {
var accountController = new AccountController(User, req.session);
console.log("here1");
var signedIn = accountController.session.userProfileModel !== undefined ? 1 : 0;
console.log("here2");

//Author search
if(req.query.author !== undefined) {
var author = req.query.author;
Post.find().where('author').equals(author).sort({ created: -1 }).limit(10).exec(function(err, authorsPosts) {
if (err) return res.send("error");
console.log("\n\nAuthorsPosts:" +authorsPosts);
console.log("\n\authorsPosts.length: " +authorsPosts.length);
console.log("authors post.constructor = " +authorsPosts.constructor);
if(authorsPosts.length==0) {
console.log("length=0");
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts,
error: "Sorry there are no posts with that tag."
});
} else {
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: authorsPosts
});
}
});
}
//Tag search
if(req.query.filter !== undefined) {
var tagList = req.query.filter.constructor == Array ? req.query.filter : req.query.filter.split(",");
Post.find( { tags : { $elemMatch: { $in : tagList } } } ).limit(10).exec(function(err, taggedPosts) {
if (err) return res.send("error");
console.log("\n\taggedPosts.length: " +taggedPosts.length);
if(taggedPosts.length==0) {
console.log("length=0");

res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: taggedPosts,
error: "Sorry there are no posts with that tag."
});
} else {
console.log("\n\ntaggedPosts:\n\n" +taggedPosts);
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: taggedPosts
});
}
});
}
//or just latest
Post.find().sort({ created: 1 }).limit(10).exec(function(err, latestPosts) {
if (err) return res.send(err);
res.render('pages/index', {
viewDataSignStatus: viewDataSignedIn[signedIn],
previews: latestPosts
});
});
});


Whats more, it isn't totally not working. When the code gets to those render calls it throws the error and the page usually freezes, you cant click any links, then if I reload once or twice it will work, and render the template with the correct data.

Also when I travel to '/' with on of these query strings e.g.
GET /?filter=Marc%20Behrens
it gets as far printing all the returned posts and then throws the error.

Thank you!

Edit: thanks Alex Ford.

new error is:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:154:12)
at ServerResponse.json (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:240:15)
at ServerResponse.send (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/response.js:142:21)
at module.exports (/Users/ben/Documents/csMSc/web/site/app/app.js:100:9)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:321:12)
at next (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:261:10)
at Layer.handle_error (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/layer.js:60:5)
at trim_prefix (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:300:13)
at /Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:321:12)
at next (/Users/ben/Documents/csMSc/web/site/app/node_modules/express/lib/router/index.js:261:10)

Answer

My guess for the "headers already sent" error is that your // or just latest code is running even when one of the above if statements runs. If that's the case then you'll surely be making multiple calls to res.render or res.send. Try this:

router.get('/', function(req, res, next) {

  /* ... */

  //Author search
  if(req.query.author !== undefined) {
    /* ... */
      if(authorsPosts.length==0) {
        res.render(/*...*/);
      } else {
        res.render(/*...*/);
      }
    /* ... */
  }
  //Tag search
  else if(req.query.filter !== undefined) {
    /* ... */
      if(taggedPosts.length==0) {
        res.render(/*...*/);
      } else {
        res.render(/*...*/);
      }
    /* ... */
  }
  else {
    //or just latest
    res.render(/*...*/);
  }
});