简体   繁体   English

Node.JS、Express 和 MongoDB Atlas:: 多个集合 ejs

[英]Node.JS, Express & MongoDB Atlas:: Multiple Collections ejs

I have tried almost all the solutions but couldn't get this fixed.我已经尝试了几乎所有的解决方案,但无法解决这个问题。

I'm trying to get multiple collection of data (Mongodb Atlas) onto one ejs file.我正在尝试将多个数据集合(Mongodb Atlas)放到一个 ejs 文件中。 But I can't get data onto the rendered ejs sheet.但是我无法将数据获取到呈现的 ejs 表上。 But when I tested with locus I can see all the data received from the database up to res.render("/vendor/show").但是当我使用 locus 进行测试时,我可以看到从数据库接收到的所有数据,直到 res.render("/vendor/show")。 But it is not passing onto ejs template.但它没有传递到 ejs 模板。

I have three sets of data sitting on the mongo Atlas:我在 mongo Atlas 上有三组数据:

const vendorSchema = new mongoose.Schema({
    name:String,
    image:String,
    imageId:String,
});

const newpromoSchema = new mongoose.Schema({
    name:String,
    image:String,
    imageId:String,
    vendor:String,
    description:String,
    sdate:String,
    edate:String,
});
const neweventSchema = new mongoose.Schema({
    name:String,
    image:String,
    imageId:String,
    description:String,
    vendor:String,
    sdate:String,
    edate:String,
});

    router.get("/vendors/:id",function(req,res) {
        var events={}; //Create Empty event Object
        var promotions={}; //Create Empty promotion Object
        var vendors={};//Create Empty vendor Objext
        Promotion.find({},function (err,allPromotions) {

            if (err) {
                console.log(err);
            } else {
                //Find Collection And Assign It To Object
                promotions=allPromotions;             
            }
        });

        Event.find({},function(err, allEvents) {
            if (err) {
                console.log(err);
            } else {      
                events=allEvents;   
            }
        });
        Vendor.find({},function(err,allVendors){
            if(err){
                console.log(err);
            }else{
                vendors=allVendors;
                //find order collection and passing it to ejs templates
                res.render("vendors/show",{ event:events, promotion:promotions vendor:vendors});
            }
        });
    });

Show.ejs code as 
<%=vendor.name%> 
<%=promotion.name%> 
<%=event.name%>

You need to handle the async process.您需要处理异步过程。 In your current code, there are three async processes.在您当前的代码中,有三个异步进程。 Please go through following the link you will definitely get an idea about it.请按照链接进行操作,您一定会对此有所了解。

https://blog.risingstack.com/mastering-async-await-in-nodejs/ https://blog.risingstack.com/mastering-async-await-in-nodejs/

if you need more help Don't hesitate to comment.如果您需要更多帮助,请随时发表评论。

I managed to get all data using the below function.我设法使用以下功能获取所有数据。 But now I have another issue, need to filter all the Promotions and Events by vendor But it doesn't filter.但是现在我有另一个问题,需要按供应商过滤所有促销和活动但它没有过滤。

Data Schema数据架构

 const neweventSchema = new mongoose.Schema({
        name:String,
        image:String,
        imageId:String,
        description:String,
        vendor:String,
        sdate:String,
        edate:String,
    });

    const Event = mongoose.model("Event",neweventSchema);

    module.exports = Event;



    //SCHEMA SETUP===============================================================================================================
    const newpromoSchema = new mongoose.Schema({
        name:String,
        image:String,
        imageId:String,
        vendor:String,
        description:String,
        sdate:String,
        edate:String,
    });
    //compile into a model
    const Promotion = mongoose.model('Promotion',newpromoSchema);

    module.exports = Promotion;

  //SCHEMA SETUP===============================================================================================================   
    const vendorSchema = new mongoose.Schema({
        name:String,
        image:String,
        imageId:String,
    });

    const Vendor = mongoose.model("Vendor", vendorSchema);

    module.exports = Vendor;

Routes as below路线如下

router.get("/vendors/:id", function (req, res) {

  Vendor.find({}, function (err, allVendors) {
    if (err) {
        console.log(err);
    } else {
      // //Find Collection And Assign It To Object

      Event.find({}, function (err, allEvents) {
        if (err) {
          console.log(err);
        } else {

          Promotion.find({}, function (err, allPromotions) {
            if (err) {
                console.log(err);
            } else {

              //find order collection and passing it to ejs templates
              res.render("vendors/show", {event: allEvents, promo: allPromotions, vendor: allVendors});

            }
          });
        }
      });
    }
  });
});

EJS Show page But It doesn't get filtered. EJS 显示页面但它没有被过滤。

    <!-- Trying to filter all the events by vendor if event's vendor name == vendor name then show on the show page -->
<%if(event.vendor === vendor.name){%>
<div class = "container-fluid">
    <div class = "row">
        <%event.forEach(function(events){%>
            <div class="col-sm-6 col col-md-4 col-lg-3">
                <div class="thumbnail">
                    <img src ="</%=events.image%>" class="rounded" width="304" height="236">
                    <div class="caption">
                        <h4><%=events.name%></h4>
                        <p class="font-italic font-weight-bold text-muted">
                            <%=events.vendor%>
                        </p>
                        <p><strong>Start Date</strong><%=events.sdate%></p>
                        <p><strong>End Date</strong><%=events.edate%></p>
                    </div>
                    <p>Event Details <br>
                        <%=events.description.substring(0,100)%>
                    </p>
                    <p>             
                        <a href="events/</%=events._id%>", class="btn btn-outline-primary btn-sm">Find More </a>
                    </p>
                </div>  
            </div>
        <%})%>
    </div>
</div>
<%}%> 
<!-- //Trying to filter all the promotions by vendor if vendor name= to promotions's vendor name only show in the show page-->
<%if(promo.vendor===vendor.name){%>
<div class = "container-fluid">
    <div class = "row">
        <%promo.forEach(function(promotions){%>
            <div class="col-sm-6 col col-md-4 col-lg-3">
                <div class="thumbnail">
                    <img src ="<%=promotions.image%>" class="rounded" width="304" height="236">
                    <div class="caption">
                        <h4><%=promotions.name%></h4>
                        <p class="font-italic font-weight-bold text-muted"> <%=promotions.vendor%></p>
                        <p><strong>Start Date</strong> <%=promotions.sdate%></p>
                        <p><strong>End Date</strong> <%=promotions.edate%></p>
                    </div>          
                    <p>
                        <%=promotions.description.substring(0,100)%> 
                    </p>
                    <p>             
                        <a href="/promotions/<%=promotions._id %>", class="btn btn-outline-primary btn-sm">Find More </a>
                    </p>
                </div>  
            </div>
        <%})%>
    </div>
</div>
<%}%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM