0

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 ?

2 Answers 2

2

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}")
0
1
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}")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.