title: Node Express ejs Error: Failed to lookup view "error" in views directory
body: <p>I am making an express app with ejs and mongoose. </p>
  
  <p>I am getting this error: </p>
  
  <pre><code> 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)   
  </code></pre>
  
  <p>from two calls to <code>res.render()</code> where the data that is passed in comes back from a mongoose query e.g.:</p>
  
  <pre><code>  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
                  });
              }
          });
      }
  </code></pre>
  
  <p>and the other one is the same but with a query of </p>
  
  <pre><code>    Post.find( { tags : { $elemMatch: { $in : tagList } } } ).limit(10).exec(function(err, taggedPosts) {
  </code></pre>
  
  <p>However all my other render calls on this view are working just fine including one later in the same function:</p>
  
  <pre><code> //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
          });
      });
  </code></pre>
  
  <p>where Im pretty sure latestPosts is in exactly the same format as authorsPosts in the one above.</p>
  
  <p>There are no calls to render a view called error. </p>
  
  <p>The error data passed to some of the calls to <code>res.render('pages/index')</code> above is passed using a custom filter</p>
  
  <pre><code>//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];
  };
  </code></pre>
  
  <p>which appears in the file app/views/pages/index.ejs as </p>
  
  <pre><code>&lt;p&gt;&lt;%=: locals | get:'error','' %&gt; &lt;/p&gt;
  </code></pre>
  
  <p>My ejs setup looks like:</p>
  
  <pre><code>// view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  app.engine('ejs', require('ejs').renderFile);
  </code></pre>
  
  <p>heres the whole offending route function in all its horrible glory </p>
  
  <pre><code>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
          });
      });
  });
  </code></pre>
  
  <p>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. </p>
  
  <p>Also when I travel to '/' with on of these query strings e.g. <code>GET /?filter=Marc%20Behrens</code> it gets as far printing all the returned posts and then throws the error.  </p>
  
  <p>Thank you!</p>
  
  <p>Edit: thanks Alex Ford. </p>
  
  <p>new error is: </p>
  
  <pre><code>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)
  </code></pre>