首页 > 解决方案 > 如何将多个嵌入集合中的数据获取到 ejs 中?

问题描述

大家好,这个问题有点傻,但我是编程新手。所以我一直在为我的大学项目开发​​一个产品管理系统,使用 ejs、nodejs、express、mongoose、mongodb。所以我无法弄清楚如何获取类别标题而不是objectId。我尝试使用<td> <%= issue.product.category.title%></td>,但它变得空白。

图片供参考。

在此处输入图像描述

Ejs 代码。

<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()
}

});

常量问题 = mongoose.model('issue',issueSchema);

module.exports = 问题;

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

产品型号

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
}

}, { 时间戳:真 });

类别模型

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

}, { 时间戳:真 });

const 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);
})

};

先感谢您!

标签: node.jsmongodbexpressmongooseejs

解决方案


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


推荐阅读