if语句:无法部分渲染

时间:2018-10-26 15:12:01

标签: javascript express express-handlebars

我正在编写一个基于用户数据在浏览器中显示不同图像的应用程序。后端工作正常:我正在获取适当的用户数据以存储在cookie中。这是hbs视图的样子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Project</title>
    <base href="/">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="./public/css/styles.css" type="text/css">
</head>
<body>
    {{{body}}}
    <div class="container">
        <h1>{{title}}</h1>
        <div class="row">
            <div class="buttons col-xs-12">
                {{#each users}}
                <button class="btn btn-primary" id={{this.User_ID}} data-toggle="modal" data-target=".modal" onclick="setUserCookie(event)">
                    User ID: {{this.User_ID}} <br>
                    Geo: {{this.Geo}} <br>
                    Industry: {{this.Industry}} <br>
                    Company Size: {{this.Company_Size}} <br>
                </button>
                {{/each}}
            </div>
        </div>
        {{>picture_view}}
    </div>
</body>
<script src="src/js/controller.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
</html>

这是部分picture_view的样子:

{{#newLogin}}
<div class="row">
    <img src="../src/images/Austin.jpg" alt="">
</div>
{{/}}

,URL的我的app.js路由如下:

app.get('/users/:id', (req, res)=>{
    let data = req
    User.findOne({User_ID: req.params.id}).then((user)=>{
        res.render('partials/picture_view', {user:user, helpers:{
                    newLogin: function(){
                        console.log('newLogin runnning')
                        let user = data.cookies.user;
                        if(user){
                            return true;
                        } else {
                            return false
                        }
                    }
                }})
        });
})

这是根页面的路由:

app.get('/', (req, res) => {
    let data = req;
        User.find({}).exec(function(err, users){
            res.render(__dirname + '/public/views/', {title: "Users", users:users, helpers:{
                    newLogin: function(){
                        console.log('newLogin runnning')
                        let user = data.cookies.user;
                        if(user){
                            return true;
                        } else {
                            return false
                        }
                    }
                }
            });
        })
    }
)

我无法获得像这样的局部渲染。如果我在浏览器中导航到/ users /:id页面,则newLogin函数将'true'值返回到该页面,但不呈现图像。我试过将模板逻辑移到首页,并且做同样的事情。我很茫然。

0 个答案:

没有答案