猫鼬填充在查询数据时不起作用

问题描述 投票:1回答:1

我有2个模型,categorystory

故事包含类别的reference id

在控制器中,我有一个函数mystories,该函数应该获取特定用户story records的所有along with category information

我正在从类别收集的故事收集but not中获取数据。

我收到的结果是这样的:

category_id: "5d10978c8e0f5d5380fdb3e6"
created_at: "2019-06-25T10:02:47.637Z"
created_by: "5d1066fba920ef2ccfe68594"
image: "uploads/1561456967615_164.jpg"
published: "no"
status: "pending"
text: "<p><strong>Fashion</strong>&nbsp;is a popular aesthetic expression in a certain time and context, especially in clothing, footwear, lifestyle, accessories, makeup, hairstyle and body&nbsp;</p>"
title: "New fashion"
__v: 0
_id: "5d11f14757f8616041616217"

但是应该返回类别收集信息,而不是类别ID。

类别模型:

const mongoose = require('mongoose');

const categorySchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: {
        type: String,
        required: true,
        unique: true
    },
    status: {
        type: String,
        required: true,
        enum: ['active','inactive','deleted']
    },
    created_at: { type: Date, default: Date.now },
    created_by: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
});

module.exports = mongoose.model('Category', categorySchema);

故事模型:

const mongoose = require('mongoose');

const storySchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: {
        type: String,
        required: true
    },
    text: {
        type: String,
        required: true
    },
    category_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Category',
        required: true
    },
    image: {
        type: String,
        required: true
    },
    status: {
        type: String,
        required: true,
        enum: ['pending','approved','deleted']
    },
    published: {
        type: String,
        required: true,
        enum: ['yes','no']
    },
    created_at: { type: Date, default: Date.now },
    created_by: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
});

module.exports = mongoose.model('Story', storySchema);

控制器代码:

const mongoose = require('mongoose');
const Story = require('../models/story');
const Category = require('../models/category');

exports.mystories = async (req, res, next) => {
    const user_id = req.userData.id;

    const all_stories = await Story
        .find({ created_by: user_id})
        .populate('name','category')
        .sort({ created_at: -1 })
        .exec();

    if(all_stories.length > 0) {
        return res.status(200).json({
            response: all_stories
        });
    }else {
        return res.status(200).json({
            response: []
        });
    }
};

exports.add_story = (req, res, next) => {
    console.log(req.file);

    const story = new Story({
        _id: new mongoose.Types.ObjectId(),
        title: req.body.story_title,
        text: req.body.story_text,
        category_id: req.body.story_category,
        image: req.file.path,
        status: 'pending',
        published: 'no',
        created_by: req.userData.id
    });

    story
        .save()
        .then(result => {
            console.log(result);
            res.status(200).json({
                response: 'added_story'
            });
        })
        .catch(err => {
            console.log(err);
            res.status(500).json({
                error: err
            })
        });
};
node.js mongodb mongoose mongodb-query
1个回答
2
投票

populate采用故事模式中给出的字段名称。应该是:

.populate({path: 'category_id', select: 'name'})

© www.soinside.com 2019 - 2024. All rights reserved.