Solve Error: MissingSchemaError: Schema hasn’t been registered for model “YourModel”.

This article gives you a comprehensive understanding of Java object serialization and deserialization>>>

When using mongodb, if you encounter the following error:

/home/ec2-user/YourProject/node_modules/mongoose/lib/index.js:391
      throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "YourModel".
Use mongoose.model(name, schema)
    at new MissingSchemaError (/home/ec2-user/YourProject/node_modules/mongoose/lib/error/missingSchema.js:20:11)
    at Mongoose.model (/home/ec2-user/YourProject/node_modules/mongoose/lib/index.js:391:13)
    at Object.<anonymous> (/home/ec2-user/YourProject/YourScript.js:4:27)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:775:12)
    at startup (internal/bootstrap/node.js:300:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)

Generally speaking, the model of mongodb database does not have a chain. At the beginning of yourscript.js, the following lines cannot be missing:

require('./yourModel.js');
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/YourDatabase");
const YourModel = mongoose.model('YourModel');

Then, the content of yourmodel.js can be as follows:

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

const YourModelSchema = new Schema({
    something1: { type: String, required: '`something1` must be filled', unique: true },
    something2: { type: String },
});

module.exports = mongoose.model('YourModel', YourModelSchema);

Similar Posts: