如何通过express将数据对象发送到ejs模板中,在同一路径上进行多次查询后得到?

时间:2017-03-17 17:39:20

标签: node.js mongodb express mongoose ejs

我想像这个原型一样回家:enter image description here

这里有四部分 - 好莱坞,英国,西班牙和意大利电影 对于所有保存电影数据我只有一个架构

 var mongoose= require('mongoose');
     var movies= mongoose.Schema({
            title:String,
            image:String,
            genre:String,//can either hollywood,italian,english or spanish
            description:{
                type:String,trim:true
            },
            release:String,
            download:String,
            youtube:String,
            upload:{
                type: Date,
                default: Date.now
            }
        });
        var Movie=mongoose.model("movie",movies);
        module.export

所以为了从主页获取数据,我使用的方法不成功。如果您有任何想法请该怎么做?

在主要路线:

router.get('/',(req,res,next)=> {
//as i want only 4 hollywood movie to appear first in home page so use limit
   var hollywood= Movie.find({genre:'hollywood'}).limit(4).sort('-upload').exec( function(err, hollywood) {
        if (err)
            console.log(err);

        else
            return hollywood;
    });


    var english= Movie.find({genre:'hollywood'}).limit(4).sort('-upload').exec( function(err, english) {
        if (err)
            console.log(err);

        else
            return english;
    });


    var spanish= Movie.find({genre:'hollywood'}).limit(4).sort('-upload').exec( function(err, spanish) {
        if (err)
            console.log(err);

        else
            return spanish;
    });

    var italian= Movie.find({'genre':'genre'}).limit(4).sort('-upload').exec( function(err, italian) {
        if (err)
            console.log(err);

        else
            return italian;
    });


res.render('index.ejs',{hollywood:hollywood,english:english,spanish:spanish,italian:italian});


});

在index.ejs

<div class="row">
                        <% hollywood.forEach(function(movie){ %>
                        <div class="col-xs-12 col-sm-4 col-md-3 col-lg-3">
                            <div class="thumbnail">
                                <img src="<%=movie.image%>" alt="">
                                <div class="caption">
                                    <h4><%=movie.title%>Movie Name</h4>
                                    <p><a href="/detail/<%=movie._id%>" class="btn btn-primary" role="button">Download</a></p>
                                </div>
                            </div>
                        </div>

                        <%})%>


<div class="row">
                        <% english.forEach(function(movie){ %>
                        <div class="col-xs-12 col-sm-4 col-md-3 col-lg-3">
                            <div class="thumbnail">
                                <img src="<%=movie.image%>" alt="">
                                <div class="caption">
                                    <h4><%=movie.title%>Movie Name</h4>
                                    <p><a href="/detail/<%=movie._id%>" class="btn btn-primary" role="button">Download</a></p>
                                </div>
                            </div>
                        </div>

                        <%})%>

<div class="row">
                        <% spanish.forEach(function(movie){ %>
                        <div class="col-xs-12 col-sm-4 col-md-3 col-lg-3">
                            <div class="thumbnail">
                                <img src="<%=movie.image%>" alt="">
                                <div class="caption">
                                    <h4><%=movie.title%>Movie Name</h4>
                                    <p><a href="/detail/<%=movie._id%>" class="btn btn-primary" role="button">Download</a></p>
                                </div>
                            </div>
                        </div>

                        <%})%>

<div class="row">
                        <% italian.forEach(function(movie){ %>
                        <div class="col-xs-12 col-sm-4 col-md-3 col-lg-3">
                            <div class="thumbnail">
                                <img src="<%=movie.image%>" alt="">
                                <div class="caption">
                                    <h4><%=movie.title%>Movie Name</h4>
                                    <p><a href="/detail/<%=movie._id%>" class="btn btn-primary" role="button">Download</a></p>
                                </div>
                            </div>
                        </div>

                        <%})%>

0 个答案:

没有答案