如何从多个嵌入的collections中获取数据到ejs?
How to get the data from multiple embbedded collections into ejs?
大家好,这个问题有点傻,但我是编程新手。因此,我一直在使用 ejs、nodejs、express、mongoose mongodb 为我的大学项目开发产品管理系统。所以我无法弄清楚如何获取类别标题而不是 objectId。我尝试使用 <td> <%= issue.product.category.title%></td>
但它变成空白。
图片供参考。
enter image description here
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()
}
});
const Issue = mongoose.model('issue',issueSchema);
module.exports = 问题;
这里,我嵌入了产品collection。
产品型号
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
}
}, { 时间戳: true });
类别模型
const CategorySchema = new Schema({
title: {
type: String,
required: true
}
}, { 时间戳: true });
常量类别 = mongoose.model('category',CategorySchema);
module.exports = 类别;
现在这是我获取数据的 issueController。
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);
})
};
提前谢谢你!
单从代码上说有点难。
console.log 你从数据库中得到的东西会有帮助,所以在 ejs 的头部只是 pus
<% console.log(issues) %>
这会将返回的对象显示到您的视图中,您将从那里看到返回的内容,然后您可以在对象中引用正确的元素。
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);
})
大家好,这个问题有点傻,但我是编程新手。因此,我一直在使用 ejs、nodejs、express、mongoose mongodb 为我的大学项目开发产品管理系统。所以我无法弄清楚如何获取类别标题而不是 objectId。我尝试使用 <td> <%= issue.product.category.title%></td>
但它变成空白。
图片供参考。
enter image description here
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()
}
});
const Issue = mongoose.model('issue',issueSchema);
module.exports = 问题;
这里,我嵌入了产品collection。
产品型号
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
}
}, { 时间戳: true });
类别模型
const CategorySchema = new Schema({
title: {
type: String,
required: true
}
}, { 时间戳: true });
常量类别 = mongoose.model('category',CategorySchema);
module.exports = 类别;
现在这是我获取数据的 issueController。
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);
})
};
提前谢谢你!
单从代码上说有点难。
console.log 你从数据库中得到的东西会有帮助,所以在 ejs 的头部只是 pus
<% console.log(issues) %>
这会将返回的对象显示到您的视图中,您将从那里看到返回的内容,然后您可以在对象中引用正确的元素。
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);
})