6

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;
Trisitian
  • 71
  • 1
  • 1
  • 6
  • 1
    Export the Schema as a Schema. Not a mongoose model. module.exports = CharacterSchema; – Jason Feb 27 '21 at 17:20

2 Answers2

17

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

apaternina
  • 381
  • 5
  • 14
3

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;
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • That's really emberrising, although when fixing the model name it's still throwing an error Invalid schema configuration: `model` is not a valid type within the array `characters` – Trisitian Dec 02 '19 at 02:34
  • is your characters model is used in other place or just in user model ? – Saurabh Mistry Dec 02 '19 at 02:35
  • The error is generating from The schema, however I do use it in my app.js in a post route that looks like this ` User.find({'name': req.params.user}) .then(results => { const UserToPush = new User(results); console.log(UserToPush); UserToPush.characters.push(newCharacter); UserToPush.save(); res.status(201).send(newCharacter); }) .catch(err =>{ console.error(err); }) ` – Trisitian Dec 02 '19 at 02:37
  • well that fixes it but then how would I create a character? or access a characterSchema if they are in the same file – Trisitian Dec 02 '19 at 02:46
  • characters are stored in User schema – Saurabh Mistry Dec 02 '19 at 04:15
  • how do you set a default value to the type: [CharacterSchema] ? – Fiehra Feb 15 '22 at 21:36