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;
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.
Try to do the import like this way const {CharacterSchema} = require (__dirname +'/CharacterModel.js').schema; Adding the .schema at the end.
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.
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With