无效的架构配置:`model` 不是数组 `characters` 中的有效类型 [英] Invalid schema configuration: `model` is not a valid type within the array `characters`

查看:46
本文介绍了无效的架构配置:`model` 不是数组 `characters` 中的有效类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建架构子文档,但遇到上面列出的错误,有问题的模式看起来像这样架构引发问题

I'm trying to create a schema subdocument but am getting the error listed above, The schemas in question look like this Schema casuing issues

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

const CharacterSchema = new Schema();
CharacterSchema.add({
    name: {
        type: String,
        required: true
    },
    title: {
        type: String
    },
    charcterClass: { // will be limited in form creation
        type: String
    },
    level: {
        type: Number
    }
});

const Charcter = mongoose.model('User', CharacterSchema);
module.exports = Charcter;

架构调用上面的架构

const mongoose = require ('mongoose');
const Schema = mongoose.Schema;
const {CharacterSchema} = require(__dirname +'/CharacterModel.js');

const UserSchema = new Schema()
UserSchema.add({
    name: {
        type: String, 
        required: true
    } ,
    characters: [CharacterSchema]
});

const User = mongoose.model('Character', UserSchema);
module.exports = User;

推荐答案

试着像这样导入const {CharacterSchema} = require(__dirname +'/CharacterModel.js').schema;

在最后添加.schema.

这篇文章与你的问题有关,你会在那里看到解释.​​

This post is related to your issue, you will see the explanation there.

嵌入模式出错

这篇关于无效的架构配置:`model` 不是数组 `characters` 中的有效类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆