tetutato tetutato - 4 months ago 25
Node.js Question

Variable not available in script tag

I'm using passportjs to do user authentication on my express app. But I've ran into a strange problem:

On my index route I'm rendering the index.jade template with the user parameter

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


Now in my jade template I have the following

block content
if user
h1 logged in #{user}
script var shared_resources.user = user
if !user
h1 Not logged in


For some reason, the conditional pass fine in that user is there. And also when I print out the user object in logged in #{user} it prints out the details fine. However inside the script tag, I'm getting an uncaught reference, user is not defined. Does anyone know why this is happening?

Answer

You need to use template string in scrip tag as well.

script var shared_resources.user = "#{user}"
Comments