2

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: 'bob@example.com' }

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?

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
cavpollo
  • 4,071
  • 2
  • 40
  • 64

1 Answers1

5

Try to change

<% user %>

to

<%= user %>

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

Laura
  • 3,233
  • 3
  • 28
  • 45
  • Huh... thanks. I was missing the '=' as you pointed out, hehe. I guess I was too tired to notice the difference between <% and <%=. Cheers – cavpollo Dec 18 '14 at 16:41