0

这本质上是这里问题的延续:Nodejs Passport display username

app.get('/hello', function(req, res) {
    res.render('index.jade', { name: req.user.username });
});

所以用户通过 PassportJS 登录,然后转到正文index.jade中包含#{name}的 ,它将被替换为 的值req.user.username

问题:是否可以使用req.user.usernameinindex.jade的 JavaScript 的值?我尝试将其值分配给变量,但它不起作用。

我一直在使用隐藏输入#{name}作为值的技巧:

input(type='hidden', id='variableName', value='#{name}')

然后 JavaScript 可以使用以下方法访问该值:

$("#variableName").val()

这行得通。但它是否有任何潜在的缺点,如安全问题?这样做的正确方法是什么?

4

1 回答 1

2

You have a few options. One of them is what you did and put the value inside you html. You can also solve it by doing:

script
   window.name = #{name};

This will create an inline script that sets the variable. The other option you have is using ajax. That means you probably need to make an extra route to reply to that request.

于 2012-05-18T09:59:44.793 回答