架构配置无效:“...”不是路径“ref”处的有效类型

oca*_*avo 4 mongoose mongodb node.js

您好,我想使用嵌入式填充
并在配方模型中保留成分数组
,但
在搜索一些网站时出现此错误,但找不到答案

    throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
    ^

TypeError: Invalid schema configuration: `Ingredient` is not a valid type at path `ref`. 

Run Code Online (Sandbox Code Playgroud)

在这里食谱模型,我添加了成分字段并给出了 objectId 类型和 ref 但代码在 ref 中给出了错误

const mongoose = require('mongoose');
const recipeSchema = new mongoose.Schema({
    recipeName: {
        type: String,
        required: true,
        min: 2,
        max: 255,
    },
    addedUser: {
        type: {
            tpye: String,
            required: true,
            min: 2,
            max: 255,
        }

    },
    ingredients: [{
        tpye: mongoose.Schema.Types.ObjectId,
        ref: 'Ingredient'
    }]

});
module.exports = mongoose.model('Recipe', recipeSchema);
Run Code Online (Sandbox Code Playgroud)

成分型号

const mongoose = require('mongoose');

const ingredientSchema = new mongoose.Schema({
    ingredientName: {
        type: String,
        required: true,
        min: 2,
        max: 255,
    },
    quantity: {
        type: {
            tpye: Number,
            required: true,
            min: 0
        }
    },
    unit: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Unit'
    }
});
module.exports = mongoose.model('Ingredient', ingredientSchema);
Run Code Online (Sandbox Code Playgroud)

小智 6

您在控制器中的“成分:”和“添加用户:”中的“类型”,其写为“tpye”。尝试改变这一点!