通过带有节点的客户端上的按钮获取用户信息

时间:2019-03-20 19:48:10

标签: html node.js mongoose ejs

我正在创建一个博客站点,您可以在其中创建自己的博客并观看其他人的博客,我有一个页面,您可以在其中看到博客列表并输入每个博客,我正在使用ejs并使用for循环添加每个博客的li标签,这里是ejs和html:

<%- include('partials/header') %>
    <h1><%= title %></h1>
    <ul class="list-group">
    <% users.forEach(function(Tuser) { %>
        <% if(Tuser.id != user.id) { %>
            <li class="list-group-item">
                <a href="/posts/othersblog"><h5><%= Tuser.username %></h5></a>
            </li>
        <% } %>
    <% }) %>
    </ul>
<%- include('partials/footer') %>

Tuser是不是您的每个用户,单击我想要获取Tuser的ID或用户名的标签,这是节点部分中的app.post:

//get someone else's blog
router.get('/othersblog',(req,res) => {
    res.render('othersblog',{
        headline: "Website | User Blog",
        title: "Blog",
        Tuser: req.Tuser,
    })
})

我如何获得Tuser,以便可以在othersblog页面(其他人的博客)中使用它

1 个答案:

答案 0 :(得分:1)

您可以在路线上使用GET参数,如下所示:

在您的模板上

monthlyReturn

然后在您的路线上

<% users.forEach(function(Tuser) { %>
   <% if(Tuser.id != user.id) { %>
       <li class="list-group-item">
           <a href="<%= '/posts/othersblog/' + Tuser.username %>"><h5><%= Tuser.username %></h5></a>
       </li>
   <% } %>
<% }); %>