简体   繁体   中英

Why is this variable not getting passed to jade from my express app?

My express app has got the following code:

app.get('/dashboard', function (req, res) {
    Student.findOne({
        email: req.session.email
    }).exec(function (err, studentData) {
        if (studentData) {
            console.log(studentData);
            res.render('dashboard', {
                info:{
                    username: studentData.username,
                    email: studentData.email,
                    roll: studentData.roll,
                    department: studentData.department,
                    regYear: studentData.regYear
                }
            });
        } else {
            res.render('index');
        }
    });
});

And my jade template has got the following code:

doctype html
html
    head
        meta(charset='UTF-8')
        title I N C U B A T I O N
        link(rel='stylesheet', href='/bower_components/Materialize/bin/materialize.css')
        script(src='/bower_components/jquery/dist/jquery.min.js')
        script(src='/bower_components/Materialize/bin/materialize.js')
        link(rel='stylesheet', type='text/css', href='css/main.css')
        script.
            console.log(!{locals.info.username});

    body
        ul#slide-out.side-nav.fixed
            li
                .row.valign-wrapper.profile_picture
                    .col.s3
                    .col.s6
                        img.circle.responsive-img(src='http://1.bp.blogspot.com/_Mt4qyhflsHY/S8wCjflidQI/AAAAAAAAAhI/FiTJiysCYus/s1600/gravatar.jpg', alt='')
                        // notice the "circle" class
                    .col.s3
            li
                .row.valign-wrapper
                    p #{info.username}

        a.button-collapse(href='#', data-activates='slide-out')
            i.mdi-navigation-menu

I want to pass on the variables from my node app to the template but it's not getting passed.
What am I doing wrong here?

Have you set your view engine to jade in express?

app.set('view engine', 'jade');

http://expressjs.com/en/guide/using-template-engines.html

You don't have to prefix the variables with locals. in jade. From the structure of your call to render , it looks like you're setting info.username , not locals.info.username .

This means you should see your variable with

console.log(!{info.username});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM