【发布时间】:2020-07-27 14:20:33
【问题描述】:
我正在使用Express、EJS 和 MongoDB 开发 blogging application(单击链接以查看 GitHub 存储库)。
我有 Posts 和 Post Categories,每个都在自己的集合中。
类别架构:
const mongoose = require('mongoose');
const categorySchema = new mongoose.Schema({
cat_name: {
type: String,
required: true
},
updated_at: {
type: Date,
default: Date.now()
},
created_at: {
type: Date,
default: Date.now()
}
});
module.exports = mongoose.model('Category', categorySchema);
帖子架构:
const mongoose = require('mongoose');
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
short_description: {
type: String,
required: true
},
full_text: {
type: String,
required: true
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'category'
},
post_image: {
type: String,
required: false
},
updated_at: {
type: Date,
default: Date.now()
},
created_at: {
type: Date,
default: Date.now()
}
});
module.exports = mongoose.model('Post', postSchema);
显示帖子的控制器方法:
exports.displayDashboard = (req, res, next) => {
const posts = Post.find({}, (err, posts) => {
if(err){
console.log('Error: ', err);
} else {
res.render('admin/index', {
layout: 'admin/layout',
website_name: 'MEAN Blog',
page_heading: 'Dashboard',
posts: posts
});
}
}).populate({ path: 'category', model: Category })
};
如果在视图中我有:
<table class="table table-striped table-sm mb-0">
<thead>
<tr class="d-flex">
<th class="col-1 pl-2">#</th>
<th class="col-2">Title</th>
<th class="col-2">Category</th>
<th class="col-2">Created</th>
<th class="col-2">Updated</th>
<th class="col-3 pr-2 text-right">Actions</th>
</tr>
</thead>
<tbody>
<% if (posts) { %>
<% posts.forEach(function(post, index) { %>
<tr data-id="<%= post._id %>" class="d-flex">
<td class="col-1 pl-2">
<%= index + 1 %>
</td>
<td class="col-2">
<a href="/<%= post._id %>" class="text-dark">
<%= post.title %>
</a>
</td>
<td class="col-2">
<%= post.category %>
</td>
<td class="col-2">
<%= post.created_at.toDateString(); %>
</td>
<td class="col-2">
<%= post.updated_at.toDateString(); %>
</td>
<td class="col-3 text-right pr-2">
<div class="btn-group">
<a href="../dashboard/post/edit/<%= post._id %>" class="btn btn-sm btn-success">Edit</a>
<a href="#" class="btn btn-sm btn-danger delete-post" data-id="<%= post._id %>">Delete</a>
</div>
</td>
</tr>
<% }); %>
<% } else { %>
<tr>
<td colspan="5">There are no posts</td>
</tr>
<% } %>
</tbody>
</table>
一个对象显示在 Category 列(posts 表的):
然而,我尝试使用 <%= post.category.cat_name %> 显示 类别名称 我收到此错误:
Cannot read property 'cat_name' of null
我做错了什么?
【问题讨论】:
-
首先,猫鼬将您的架构在内部转换为小写字母(例如“类别”=> 类别)。因此,postSchema 中的类别应引用“类别”,因此请尝试。你有没有测试过
post._id之外的其他字段是否可以成功渲染? -
@rags2riches 我试过了。 t 不是那样的。
标签: javascript mongodb mongoose mongoose-populate