为什么MongoDB填充方法在此应用程序中失败? [英] Why does MongoDB populate method fail in this application?
问题描述
我正在使用 博客应用程序 (点击指向请参见 GitHub 存储库),其中 Express ,
I am working on a blogging application (click the link to see the GitHub repo) with Express, EJS and MongoDB.
我有帖子和帖子类别,它们都在自己的收藏集中.
I have Posts and Post Categories, each in its own collection.
类别架构:
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>
一个对象显示在帖子表的类别列中:
an object is displayed in The Category column (of the posts table):
但是,如果我尝试使用<%= post.category.cat_name %>
显示类别名称,则会出现此错误:
Yet, ig I try to display the category name using <%= post.category.cat_name %>
I get this error:
Cannot read property 'cat_name' of null
我在做什么错了?
推荐答案
我发现了问题所在:我已经删除了帖子的先前分配的类别.修复很简单(在视图中):
I found out what the problem was: I had deleted a category previously assigned to a post. The fix is simple (in the view):
<%= post.category != null ? post.category.cat_name : 'No category assigned' %>
代替
<%= post.category.cat_name %>
非常感谢参与其中的每个人.
Many thanks to everyone that got involved for the help.
这篇关于为什么MongoDB填充方法在此应用程序中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!