node.js - 如何将多个嵌入集合中的数据获取到 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);
})
};
先感谢您!
解决方案
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);
})
推荐阅读
- spring-boot - 迁移到 Spring 2.1.6 后 HttpServletRequest getLocalAddr 不返回本地地址
- angular - 离子通知 - 网络服务 Appelle
- algorithm - 将增量网格索引转换为 (x,y) 坐标
- ag-grid - 从 ag-grid 18.1.2 升级到 ag-grid-community 20.1.0 后缺少 BaseFilter、SerializedTextFilter 等
- mapkit - 绘制的折线显示在建筑物下方
- c# - 使用 tinyurl API 获取缩短的 URL 时出错
- c# - 使用 ocelot 实现 Apigateway 的问题
- javascript - axios.delete 正在从 firebase 数据库中删除所有节点
- javascript - 如何“捕捉滚动”并在捕捉时触发事件?
- sql - MERGE 语句是否可以从 BigQuery 字段聚类中受益?