简体   繁体   中英

throw new mongoose.Error.MissingSchemaError(name) MissingSchemaError: Schema hasn't been registered for model “superheros”

I'm getting this error when I update my server on nodemon.

C:\Users\mikae\Desktop\Project\node-express-swig-mongo\node_modules\mongoose\lib\index.js:523
      throw new mongoose.Error.MissingSchemaError(name);
      ^

MissingSchemaError: Schema hasn't been registered for model "superheros".
Use mongoose.model(name, schema)
    at Mongoose.model (C:\Users\mikae\Desktop\Project\node-express-swig-mongo\node_modules\mongoose\lib\index.js:523:13)
    at Object.<anonymous> (C:\Users\mikae\Desktop\Project\node-express-swig-mongo\routes\api.js:4:26)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:903:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\mikae\Desktop\Project\node-express-swig-mongo\app.js:6:11)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:903:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\mikae\Desktop\Project\node-express-swig-mongo\bin\www:7:11)
[nodemon] app crashed - waiting for file changes before starting...

api.js

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Superhero = mongoose.model('superheros');


router.get('/superheros', function(req, res) {
  Superhero.find(function(err, superheros){
    console.log(superheros)
    res.render(
      'api',
      {title : 'Superhero API', superheros : superheros}
    );
  });
});

router.post('/superheros', function(req, res) {
  console.log(req.body.name);
  res.redirect('/api/superheros');
});

module.exports = router;

database.js

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

var Superhero = new Schema(
  {name : String}
);

mongoose.model('superheros', Superhero);

mongoose.connect('mongodb://localhost/node-superhero');

I'm building Crud with Mongoose, Node, and Express. This error appears when I write method POST on api.js and even erasing error remains. I'm learning from this tutorial https://mherman.org/blog/node-express-swig-mongo-primer/

The short answer to your question is that you are not exporting the Schema in database.js

You should do:

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

var superheroSchema = new Schema(
  {name : String}
);

module.exports = mongoose.model('Superhero', superheroSchema);

Also it is better to seprate the connection to the database in a different file or atleast use it in your main file like this:

mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true })
.then(//Some code here...)
.catch(error => handleError(error));

It is simply wrong to try and make a connection to the database inside a model definition.

And when requiring the schema use:

var Superhero = require('{path to the file containing the schema}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM