Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid schema configuration: `model` is not a valid type within the array `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;

Schema calling schema above

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;
like image 852
Trisitian Avatar asked Dec 02 '19 02:12

Trisitian


People also ask

Why is a standard module not a valid type?

A module is not a valid type. A standard module doesn't represent a class and can't be instantiated in the form of a variable. This error has the following cause and solution: You used the name of a standard module in a Dim or Set declaration. Check the spelling of the module name and make sure it corresponds to a form, MDI form, or class module.

How do I import a character model into a schema?

Try to do the import like this way const {CharacterSchema} = require (__dirname +'/CharacterModel.js').schema; Adding the .schema at the end.

Why can't I instantiate a standard module in a variable?

A standard moduledoesn't represent a classand can't be instantiated in the form of a variable. This error has the following cause and solution: You used the name of a standard module in a Dimor Setdeclaration. Check the spelling of the module name and make sure it corresponds to a form, MDI form, or class module.


Video Answer


2 Answers

Try to do the import like this way const {CharacterSchema} = require(__dirname +'/CharacterModel.js').schema;

Adding the .schema at the end.

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

Embedding schemas is giving error

like image 102
apaternina Avatar answered Nov 11 '22 18:11

apaternina


UserSchema :

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

const CharacterSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    title: {
        type: String
    },
    charcterClass: { 
        type: String
    },
    level: {
        type: Number
    }
});

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

const User = mongoose.model('User', UserSchema);
module.exports = User;
like image 20
Saurabh Mistry Avatar answered Nov 11 '22 20:11

Saurabh Mistry