Nicola
Nicola

Reputation: 475

res.render options parameters error

I'm using Node.js, Express and Jade. I use res.render with many parameters I send to the .jade page that is going to be opened:

res.render('home', { title: '/home' , css: '/stylesheets/home.css', myvar:'false', myvar2:'notTrueOrFalse'});

into the .jade page I use those values, but if I use one of them into a javascript script it doesn't work, except it is a value like "false" or "true". The console says: "Uncaught ReferenceError: notTrueOrFalse is not defined" Why?

This is the .jade file:

extends layout

block content   
#contenitore
    script.
        alert(#{myvar})     //THIS WORK
        alert(#{myvar2})    //THIS DOESN'T WORK
        var valore=checkAutentication(#{myvar})   //THIS WORK
        var valore=checkAutentication(#{myvar2})   //THIS DOESN'T WORK
        alert(#{title})      //THIS DOESN'T WORK
    #titolo
        p #{title}          //OUT OF THE SCRIPT THIS WORK

For the "title" variable the console says: "Uncaught SyntaxError: Invalid regular expression: missing /"

What's the problem? Why can't I use variables passed with res.render into a script ?

Upvotes: 0

Views: 2181

Answers (2)

Andreas Hultgren
Andreas Hultgren

Reputation: 14953

alert(#{myvar2})

Will be send to the client as:

alert(notTrueOrFalse);

Since it should be a string rather than a variable, this fails. The solution should be to surround it with quotes:

alert("#{myvar2}")

Upvotes: 1

user1741851
user1741851

Reputation:

Lets take a moment and look at your jade. We'll convert it to HTML by replacing variables.

alert(false);
alert(notTrueOrFalse);

alert(false) works because it is treated as a boolean. But without quotes notTrueOrFalse is treated as a variable. So undefined variable error.

Try

alert("#{myvar2}")

Upvotes: 2

Related Questions