试图将一个对象填充到另一个对象内的另一个对象内 (NODE.JS)

Trying to populate an object inside another object that is inside another object (NODE.JS)

此函数获取特定 "process" 并在 json 中显示以下信息:(POSTMAN)

{
    "process": {
        "_id": "5c18e8d1d4817811839d43d2",
        "name": "Dyeing",
        "colour": {
            "_id": "5c18c972b39bb20769288e8f",
            "name": "azul",
            "category": "5c18c09f4c6baf05ea621bca",
            "__v": 0
        },
        "__v": 0
    },
    "request": {
        "type": "GET",
        "url": "http://localhost:3000/process"
    }
}

过程控制器函数

   exports.process_get_process = (req, res, next) => {
    Process.findById(req.params.processId)
      .populate("colour")
      .populate("category")
      .exec()
      .then(process => {
        if (!process) {
          return res.status(404).json({
            message: "Process not found"
          });
        }
        res.status(200).json({
          process: process,
          request: {
            type: "GET",
            url: "http://localhost:3000/process"
          }
        });
      })
      .catch(err => {
        res.status(500).json({
          error: err
        });
      });
};

"process" 的模型是以下架构:

const mongoose = require('mongoose');

const processSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: { type: String, required: true },
    price: { type: Number, required: false },
    colour: { type: mongoose.Schema.Types.ObjectId, ref: 'Colour', required: false },
});

module.exports = mongoose.model('Process', processSchema);

这是颜色模型: 如您所见,对象 "category" 在 "colour" 中,我也想在 "process" 对象中显示他。

const mongoose = require('mongoose');

const colourSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: { type: String, required: true },
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true },
});

module.exports = mongoose.model('Colour', colourSchema);

问题:在"colour"里面有一个"category"对象,但是只显示分类id,我想让他显示所有的分类信息.我该如何填充它?

您可以将其指定为 populate 函数的选项。

Process.findById(req.params.processId)
      .populate({
           path: 'colour',
           populate: { path: 'category' }
       })
      .exec()