Error in express JS project

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

var Employee = mongoose.model('Employee')

should probably go something like

var Employee = require('../models/Employee')

did not work with this change.

same error.

updated code here

what has gone wrong ?

Where are you seeing this error?

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.

1 Like

I did all these changes.in my code

here is the updated code

yes.

when I run this code
>nodemon index.js

I get this error in console

\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)

Can you please suggest ?

I’m not getting that error either. I can run the revised code up and am getting screens as follows:

I know it’s a basic question, but have you run mongod in the CLI?

As an aside, it would be worth creating a .gitignore file and including the following line in it

node_modules/

You don’t need to to include those in your repo, as they’ll be pulled in when someone runs npm i

1 Like

where to put this file ? which folder ?

.gitignore goes in the root of your project. Github gives you the option of creating one when you start a new repository too.

There’s some details about it at .gitignore documentation but all you need is the name of the file or folder that you don’t want github to track.

.gitignore

node_modules/

That’s it.

1 Like

update:

bingo …I am able to open home page now ! …also added gitignore now :slight_smile:

2 Likes

got it. Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.