Javascript 为什么MongoDB填充方法在此应用程序中失败?

Javascript 为什么MongoDB填充方法在此应用程序中失败?,javascript,mongodb,mongoose,mongoose-populate,Javascript,Mongodb,Mongoose,Mongoose Populate,我正在使用、和MongoDB开发一个(单击链接查看GitHub回购) 我有帖子和帖子类别,每个都有自己的收藏 类别架构: const mongoose = require('mongoose'); const categorySchema = new mongoose.Schema({ cat_name: { type: String, required: true }, updated_at: { type: Date,

我正在使用、和MongoDB开发一个(单击链接查看GitHub回购)

我有帖子和帖子类别,每个都有自己的收藏

类别架构:

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);
Posts架构:

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>

我做错了什么?

使用async/await运算符,因为
populate
返回一个可以等待的值。更多信息和代码示例可以在官方的猫鼬网站上找到

异步函数youraweome函数(req、res、next){
试一试{
const posts=等待Post
.find({})
.populate({path:'category'});
res.render('path/to/layout',{posts:posts});
}捕获(e){
//TODO:处理可能的错误。。。
}
};

使用异步/await运算符,因为
填充
返回一个可等待的。更多信息和代码示例可以在官方的猫鼬网站上找到

异步函数youraweome函数(req、res、next){
试一试{
const posts=等待Post
.find({})
.populate({path:'category'});
res.render('path/to/layout',{posts:posts});
}捕获(e){
//TODO:处理可能的错误。。。
}
};

您可以遵循此代码

您的型号名称是:Category


您可以按照此代码操作

您的型号名称是:Category


我发现了问题所在:我删除了以前分配给某个帖子的类别。修复很简单(在视图中):


而不是

<%= post.category.cat_name %>


非常感谢所有参与帮助的人。

我发现了问题所在:我删除了以前分配给帖子的类别。修复很简单(在视图中):


而不是

<%= post.category.cat_name %>


非常感谢所有参与帮助的人。

首先,mongoose将您的模式内部转换为小写字母(例如“Category”=>categories)。因此,postSchema中的类别应该引用“categories”,所以请尝试这样做。你有没有测试过在
post.\u id
旁边是否可以成功渲染任何其他字段?@rags2riches我试过了。不是这样。首先,mongoose在内部将您的模式转换为小写字母(例如“Category”=>categories)。因此,postSchema中的类别应该引用“categories”,所以请尝试这样做。你有没有测试过在
post.\u id
旁边是否可以成功渲染任何其他字段?@rags2riches我试过了。不是这样。你可以使用async await。我应该怎么做?我发现了问题并发布了一个解决方案。谢谢你的帮助。你可以使用async Wait。我该怎么做呢?我发现了问题并发布了一个解决方案。谢谢你的帮助。我发现了这个问题并发布了一个解决方案。如果有兴趣,请查看。谢谢你的帮助。我发现了这个问题并贴了一个解决方案。如果有兴趣,请查看。谢谢你帮助我。
<%= post.category != null ? post.category.cat_name : 'No category assigned' %>
<%= post.category.cat_name %>