在猫鼬无法深度填充?

时间:2021-05-15 15:11:12

标签: node.js mongodb mongoose

所以我试图从 mongoDB 中搜索具有类别的产品。我想找到与输入的关键字(即类别)相关联的所有产品。我已将我的产品与类别相关联。一旦成功找到类别,我就可以将产品添加到类别中。此外,我想填充产品中存在的子类别和品牌。我无法填充这些。我不知道我做错了什么。所以请帮助我..谢谢!

过滤控制器

exports.getFilterCategory = (req, res, next) => {
  let regex = new RegExp(req.params.category, "i")
  Category.find({ category: regex }, { category: 2 })
    .populate({
      path: "product",
      populate: {
        path: "subCategory",
        path: "brand",
      },
    })
    .sort({ created_at: -1 })
    .limit(10)

    .then((category) => {
      console.log("hello" + category);
      if (!category) {
        console.log("not found");
      } else {
        res.render("vendor/product/product", {
          pageName: req.params.category + " Search",
          products: category,
        });
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

**产品型号:**

const mongoose = require('mongoose')

const schema = mongoose.Schema

const productSchema = new schema(
  {
    category: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Category",
      },
    ],
    subCategory: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "SubCategory",
      },
    ],
    brand: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Brand",
      },
    ],
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      required: true,
    },
    price: {
      type: Number,
      required: true,
    },
    discount: {
      type: Number,
    },
    image: {
      type: [String],
      required: true,
    },
    feature: {
      type: Boolean,
      /* required: true, */
    },
    active: {
      type: Boolean,
      /* required: true, */
    },
    stock: {
      type: Boolean,
      /* required: true, */
    },
  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model('Product', productSchema)

类别模型:

const mongoose = require('mongoose')

const schema = mongoose.Schema

const categorySchema = new schema(
  {
    category: {
      type: String,
      required: true,
    },
    image: {
      type: String,
      required: true,
    },
    subCategory: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "SubCategory",
      },
    ],
    product: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Product",
      },
    ],
  },
  {
    timestamps: true,
  }
);

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

**输出:**

hello{
  product: [
    {
      category: [Array],
      subCategory: [Array],
      brand: [Array],
      image: [Array],
      _id: 609fc2f1b291812088a95122,
      title: 'hello',
      description: 'hello',
      price: 200,
      discount: 2,
      active: true,
      stock: true,
      createdAt: 2021-05-15T12:47:45.841Z,
      updatedAt: 2021-05-15T12:47:45.841Z,
      __v: 0
    },
    {
      category: [Array],
      subCategory: [Array],
      brand: [Array],
      image: [Array],
      _id: 609fd2dfc7ced529c8cad68f,
      title: 'hello',
      description: 'hello',
      price: 200,
      discount: 2,
      active: true,
      stock: true,
      createdAt: 2021-05-15T13:55:43.417Z,
      updatedAt: 2021-05-15T13:55:43.417Z,
      __v: 0
    }
  ],
  _id: 609fc29db291812088a9511f,
  category: 'Electronics'
}

在此输出中,我想要子类别和品牌信息,而不是显示 [array]。

0 个答案:

没有答案