猫鼬填充返回的数据而没有填充

时间:2019-07-18 12:23:38

标签: node.js mongodb mongoose mean mongoose-populate

我知道这个问题被问过很多次了,但我仍然找不到解决方法

这是我的数据库

产品:
enter image description here

成分:

enter image description here

模式: Ingredient.js

    const mongoose = require("mongoose");
var IngredientSchema = new mongoose.Schema({
  quantity: {
    value: Number,
    unit: String
  },
  comment: String,
  product: { type: mongoose.Schema.Types.ObjectId, ref: "Product" }
});

module.exports = mongoose.model("IngredientSchema", IngredientSchema);

product.js

const mongoose = require("mongoose");
var ProductSchema = new mongoose.Schema({
  name: {
    type: String,
    default: "",
    trim: true,
    required: "Name cannot be blank"
  },
  category: {
    type: String,
    default: "",
    trim: true
  },
  convertion: [
    {
      unit_from: String,
      unit_to: String,
      value_from: Number,
      value_to: Number
    }
  ],
  default_unit: String
});

const Product = (module.exports = mongoose.model(
  "ProductSchema",
  ProductSchema
));

这是填充函数: IngredientRoutes.route(“ /:id”)。get(function(req,res){   让id = req.params.id;

  Ingredient.findById(id)
    .populate("produit")
    .exec()
    .then(function(data) {
      console.log(data.product, "***");
    })
    .catch(function(err) {
      console.log(err);
    });
});

这是我得到的结果: 只是产品的ID而没有增加

enter image description here

有什么主意吗?

0 个答案:

没有答案