JamesE
JamesE

Reputation: 3923

Express layouts error - variable not defined

I am using express-ejs-layouts and am seeing the following error in the console log:

 >> 5|          <title><%= title %></title>
title is not defined

I do indeed have the following element defined in my layout.ejs file:

    <title><%= title %></title>

I am populating this variable from one of my route files:

router.get('/', function(req, res) {
    res.render('index', {
        title : 'Express'
    });
});

Any idea what I am missing? Thanks!

Upvotes: 3

Views: 3017

Answers (2)

ldd
ldd

Reputation: 1

in my case, I added the title variable to the res.render call, but had not restarted the server. The change was not picked up and threw the error mentioned until I restarted the server.

Something like nodemon can help avoid this problem while you are developing.

Upvotes: 0

JamesE
JamesE

Reputation: 3923

I figured this out. There was another variable (description) that was not defined. As soon as I set the 'description' variable this worked. Seems a bit odd that the error message would be for 'title' though.

Upvotes: 4

Related Questions