提问者:小点点

如何将多个嵌入集合中的数据获取到EJS中?


大家好,这个问题有点傻,但我是编程新手。所以我一直在为我的大学项目开发一个产品管理系统,使用ejs、nodejs、express、mongoose、MongoDB。所以我不知道如何获得类别标题而不是Objectid。我尝试使用<%=issue.product.category.title%>,但它为空。

在此输入图像说明

<table class="table table-bordered">
  <thead class="bg-dark text-center">
    <tr class="text-white">
      <th>Employee Name</th>  
      <th>Email</th>  
      <th>Employee Number</th>  
      <th>Contact Number</th>   
      <th>Product ID</th>
      <th>Title</th>
      <th>Manufacturer</th>
      <th>Status</th>
      <th>Category</th>
      <th>Date/Time</th>
    </tr>
  </thead>
  <tbody class="text-center">
    <% if (issue.length> 0) { %> <% issue.forEach(issue=> { %>
    <tr>
      <td> <%= issue.ename %></td> 
      <td><%= issue.email  %></td>  
      <td><%= issue.enumber %></td>  
      <td><%= issue.cnumber %></td>  
      <td><%= issue.product.prodid%></td>
      <td><%= issue.product.title%></td>
      <td><%= issue.product.manufacturer%></td>
      <td><%= issue.product.status%></td> 
      <td> <%= issue.product.category%></td>
      <td><%= issue.issueTime %> </td>
      <% }) %> <% } else { %>

      <p>There are no issue to display...</p>

      <% } %>
    </tr>
  </tbody>
</table>

const issueSchema = new Schema({
ename: {
    type: String,
    required: true
},
email: {
    type: String,
    required: true
},
enumber: {
    type: String,
    required: true
},
cnumber: {
    type: String,
    required: true
},
desig: {
    type: String,
    required: true
},
department: {
    type: String,
    required: true
},
description: {
    type: String,
    required: true
},  
product: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Prodcut'
},
issueTime: {
    type: Date,
    default: Date.now()
}

});

常量Issue=mongoose.model(“Issue”,issueSchema);

模数.出口=发行;

在这里,我有嵌入式产品集合。

const productSchema = new Schema({
prodid: {
    type: String,
    required: true
},
title: {
    type: String,
    required: true
},
manufacturer: {
    type: String,
    required: true
},
category: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'category'
},
status: {
    type: String,
    default: 'In Stock'
},
coverImage: {
    type: Buffer,
    required: true
},
coverImageType: {
    type: String,
    required: true
}

},{timestamps:true});

const CategorySchema = new Schema({
title: {
    type: String,
    required: true
}

},{timestamps:true});

常量Category=mongoose.model(“Category”,CategorySchema);

module.exports=类别;

const issue_detail = (req, res) => {
Issue.find().sort({ createdAt: -1})
.populate('product category')
.then((issue) => {
        res.render('products/issue/details', {
            issue: issue,
          })
})
.catch((err) => {
    console.log(err);
})

};

提前谢谢你!


共1个答案

匿名用户

const issue_detail = (req, res) => {
Issue.find().sort({ createdAt: -1})
.populate({path : 'product', populate : {path : 'category'}})
.then((issue) => {
        res.render('products/issue/details', {
            issue: issue,
          })
})
.catch((err) => {
    console.log(err);
})

参考:http://mongoosejs.com/docs/populate.html#deep-populate