Search code examples
node.jsmongodbmongoose

MIssingSchemaError: Schema hasn't been registered for model "Emp"


I've tried different solutions available on stack overflow but couldn't resolve this problem.

Here is my code

var mongoose = require('mongoose');

var schema = new mongoose.Schema({
    name:{
        type:String,
        required:true
    },
    email:{
        type:String,
        required:true,
        lowercase:true
    }
});
mongoose.connect('mongodb://localhost:27017/test');
//parameters are model name,schema,collection name
var Emp = mongoose.model('Emp','schema','users');

Solution

  • You are using a string in the second parameter of the model method and it requires a Schema.

    This solves your problem:

    var Emp = mongoose.model('Emp',schema,'users');
    

    These are the parameter types you must use:

    name         String   model name
    
    schema       Schema
    
    collection   String   name (optional, induced from the model name)
    
    skipInit    Boolean whether to skip initialization (defaults to false)
    

    more information here: http://mongoosejs.com/docs/api.html#index_Mongoose-model