简体   繁体   中英

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. 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. I've used the #{} syntax before and haven't had an issue. Thoughts?

You can use ES6 template literals for this.

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 ).

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