添加三个字段时,猫鼬填充将返回null

时间:2019-01-26 15:28:41

标签: node.js mongodb express mongoose populate

这是我第二次发布有关此问题的问题 用猫鼬填充,当结果为null时 我添加了三个对象。我要填充三个对象 1. oem 2.类别,(返回null) 3.子类别(返回null) 让我向您展示与此相关的代码。 oem对象在填充后显示其数据,但类别和子类别显示为空。因此,我应该添加什么顺序才能使这两个对象的数据也显示在结果中。感谢任何人都可以提供帮助。

//产品路线。

const express = require("express");
const router = express.Router();

const Product = require("../../../models/Master/Products");

const validateProductInput = require("../../../validation/master/products");

router.get("/", (req, res) => {
  Product.find()
    .populate("oem")
    .populate("category")
    .populate("subcategory")
    .then(product => res.json(product));
});

router.get("/:id", (req, res) => {
  const id = req.params.id;
  Product.findById(id)
    .populate("oem")
    .populate("category")
    .populate("subcategory")
    .then(product => res.status(200).json(product))
    .catch(err => res.status(500).json(err));
});


module.exports = router;

//产品型号

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductSchema = new Schema({
  oem: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "company",
    required: true
  },
  category: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productcategory",
    required: true
  },
  subcategory: {
    type:mongoose.Schema.Types.ObjectId,
    ref: "productsubcategory",
    required: true
  },
  modelno: {
    type: String,
    required: true
  },
  productdescription: {
    type: String,
    required: true
  },
  productspecification: {
    type: String,
    required: true
  },
  entrydate: {
    type: Date,
    default: Date.now
  }
});
module.exports = Product = mongoose.model("product", ProductSchema);

//公司型号(OEM)

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const CompanySchema = new Schema({
  companyname: {
    type: String,
    required: true
  },
  contactperson: {
    type: String,
    required: true
  },
  contactno: {
    type: Number,
    required: true
  },
  alternatecontactno: {
    type: Number
  },
  email: {
    type: String,
    required: true
  },
  fax: {
    type: Number,
    required: true
  },
  address: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Company = mongoose.model("company", CompanySchema);

// ProductCategory模型(用于productcategory)

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductCategorySchema = new Schema({
  category: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  }
});
module.exports = ProductCategory = mongoose.model(
  "productcategory",
  ProductCategorySchema
);

//产品子类别模型(用于子类别)

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductSubCategorySchema = new Schema({
  subcategory: {
    type: String,
    required: true
  },
  parentcategory: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productcategory",
    required: true
  },
  description: {
    type: String,
    required: true
  }
});
module.exports = ProductSubCategory = mongoose.model(
  "productsubcategory",
  ProductSubCategorySchema
);

结果就是这个

[
    {
        "entrydate": "2019-01-26T14:03:35.792Z",
        "_id": "5c4c68b7e1bb692ecc525735",
        "oem": {
            "date": "2019-01-21T10:49:16.679Z",
            "_id": "5c45a3ac6a3ebd2bd0b5ed48",
            "companyname": "Microsoft",
            "contactperson": "Bill Gates",
            "contactno": 8730960219,
            "alternatecontactno": 8837099070,
            "email": "microsoft@hotmail.com",
            "fax": 368965217,
            "address": "California, USA",
            "__v": 0
        },
        "category": null,
        "subcategory": null,
        "modelno": "KLJ123456",
        "productdescription": "DNSJANDSJAKLNDSJLANDSLANDASJNDSANDSA DNSAKLDNKSLANDMSKALNDMKSALNDKLASNDKALS DNASJLDNSKJALDNKJSALNDSKALNDSKA",
        "productspecification": "BDHSJDBSJAKDASKJBDNASKJBDNASKJBDNASKJBD DNSKALNDKJASLNDJKASLDNSAKLNDJSALKNDAS DNAKSLNDASKLDNSAKLNDASKJNDSKA",
        "__v": 0
    }
]

0 个答案:

没有答案