I’m getting an error in express js project when I start server .
nodemon
Error:
MissingSchemaError: Schema hasn't been registered for model "Employee".
Use mongoose.model(name, schema)
at new MissingSchemaError
Although I have registered Employee schema correctly in model.
I’m clueless with the error and need some help at this .
I have uploaded my express js project here . Could you please take a look into it ? What has gone wrong ?
Its on crud operation.with express js and mongoose
You defined that schema in models/Employee.js, but at no place are you actually importing the module, so it remains unknown to your app. So this line in EmployeeController.js
I was able to run your app and successfully create an employee.
Please note that in order for this to work, I had to rename views/employee to views/employees (note the extra “s”) and I had to move the route definition for employees to above the error handling function in app.js, as otherwise the app would hit that middleware first and return a 404 for anything employee related.
app.use('/', index);
app.use('/users', users);
app.use('/employees', employees);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
If you can let me know how to reproduce your error, I will be happy to take a further look.
\node-crud\node_modules\mongoose\lib\index.js:325
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Employee".
Use mongoose.model(name, schema)