Search code examples
node.jsexpressejsembedded-javascript

Empty parameters sent to EJS on render


Im fairly new to nodejs, and Ive been suffering trying to solve this, and so far I cant find the problem.

At some point during my application I render a EJS template with some parameters, but it seems that even though the parameters are obtained, they are empty....

I tested the most basic case:

console.log(req.user);
res.render('menu.ejs', { user: String(req.user.username) });

the console outputs:

{ id: 1,
  username: 'bob',
  password: 'secret',
  email: '[email protected]' }

So Im not sending an empty value. The menu.ejs code is the following:

<html>
    <body>
        <% user %>
    </body>
</html>

Really simple. But when the page is rendered all I get is an empty page... Even with a and some more HTML, I just keep getting and empty value for user...

At some point in my application I used passport to authenticate my user, and using flash, I managed to get a message through:

res.render('login.ejs', { message: req.flash('message') });

But that still leaves me with my menu.ejs error. Ive renamed the file, renamed the parameter passed, added more parameters, harcoded the values of the parameters passed... and I always get same the results =(

Project Dependencies:

"dependencies": {
    "express": "~4.9.x",
    "express-session": "1.9.3",
    "mysql ": "2.5.3",
    "body-parser": "1.10.0",
    "passport": "0.2.1",
    "passport-local": "1.0.0",
    "connect-flash": "0.1.1",
    "cookie-parser": "1.3.3",
    "ejs": "1.0.0"
}

Any ideas?


Solution

  • Try to change

    <% user %>
    

    to

    <%= user %>
    

    since that is the usual syntax to output a template variable as far as I know.