我将用户的属性传递给我的 pug 模板引擎,并希望将 mongodb 用户架构的值打印到我的编辑表单中。我希望它们成为输入字段的“值”,除非用户更改它。
在我的 route ,以下是生成编辑个人资料路线的代码。
User.findById(req.params.id, function(err, foundUser) {
if (err) throw err
console.log("Found user is " + foundUser);
res.render('editProfile', {currentUser: foundUser});
});
在哈巴狗模板引擎表单中,我有一个这样的字段:
label(for="firstname") First name
input.form-control#firstname(type="text", name='firstname', value="#{currentUser.firstname}")
但它实际上是将 #{currentUser.firstname} 作为值放置在输入字段中,而不是数据库中的值。我之前使用过 #{} 语法,但没有遇到问题。想法?
最佳答案
您可以为此使用 ES6 模板文字。
input.form-control#firstname(type="text", name='firstname', value=`${currentUser.firstname}`);
这些是反引号,而不是引号。就在键盘上 1 键的左侧(如果您之前没有尝试过, read this on MDN )。
关于javascript - Pug用户名插值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50010842/