如何将变量从获取请求传递到部分车把页面

how to pass a variable from a get request into a partial handlebars page

我可以从我的登录中获取一个会话变量 id 并将其设置为一个 id,但我不确定如何在页面加载到 handlebars 页面时传递该变量。

我注释掉了 res.send 我只是想看看 emailid 是否会加载,它确实加载了。我看过一些示例,但仍然不确定如何将变量直接传递到 handlebars/html.

中的 {{username}} 位置

<!--Where the city and state info will be inputted -->
<div class="row">  
  <div class="col-sm-3">  
    <form>  
      <p>Search for parking</p>   
      <input id="location-input" type="textbox" placeholder="Enter a location" />
      <br />
      <br />
<!--When Clicked will push all the selected information above to GET API responses -->         
      <button id="submit-button" value="submit">Submit</button>  
    </form>
      <br />
      <br />
      <div id="test">
        Currently Logged in as: <br /> {{username}}
      </div>
      <br /> 
  </div>

// goes to the application folder and checks to see if authenticated
 app.get("/user", application.IsAuthenticated, function(req, res) {
// to help organize, some of the handlebars are within folders   
  var username = req.user.username;
  // res.send(username);
  res.render('./users/user');
 });

使用res.render第二个参数将数据传递给视图:

res.render('./users/user', { username: username });

来自docs

res.render(查看[ locals] [ callback])

呈现 view 并将呈现的 HTML 字符串发送到客户端。可选参数:

  • locals,一个对象,其属性为 查看。
  • callback,回调函数。如果提供,方法 returns 两者 可能的错误和呈现的字符串,但不执行 自动响应。当发生错误时,该方法调用 下一个(错误)内部