简体   繁体   English

用户名的帕格插值问题

[英]Pug interpolation issue of username

I'm passing the properties of a user to my pug template engine and wanting to print the values of the mongodb user schema out to my edit form. 我正在将用户的属性传递给我的pug模板引擎,并希望将mongodb用户模式的值打印到我的编辑表单中。 I want them to be the "value" of the input fields unless the user changes it. 我希望它们成为输入字段的“值”,除非用户更改它。

In my route, here is the code which generates the edit profile route. 在我的路线中,这是生成编辑配置文件路线的代码。

User.findById(req.params.id, function(err, foundUser) {
      if (err) throw err
      console.log("Found user is " + foundUser);
      res.render('editProfile', {currentUser: foundUser});
});

In the pug template engine form, I have a field like this: 在哈巴狗模板引擎表单中,我有一个像这样的字段:

label(for="firstname") First name
input.form-control#firstname(type="text", name='firstname', value="#{currentUser.firstname}")

But it's literally placing #{currentUser.firstname} in the input field as the value instead of what is in the database. 但这实际上是在输入字段中将#{currentUser.firstname}作为值而不是数据库中的值。 I've used the #{} syntax before and haven't had an issue. 我以前使用过#{}语法,但没有遇到任何问题。 Thoughts? 思考?

You can use ES6 template literals for this. 您可以为此使用ES6模板文字。

input.form-control#firstname(type="text", name='firstname', value=`${currentUser.firstname}`); 

Those are backticks, not quotes. 这些是反引号,而不是引号。 Just to the left of your 1 key, on your keyboard (if you haven't tried them before, read this on MDN ). 在键盘上1号键的左侧(如果您以前从未尝试过, 请在MDN上阅读 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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