I have a typical Project with Node.js - Express 3 - MongoDB
I'm trying to make a query to my model 'Tweet' in my /routes/index.js and when I run my app crashed
24 Aug 11:35:07 - [nodemon] starting `node app.js`
/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Teewt".
Use mongoose.model(name, schema)
at Mongoose.model (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286:13)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/routes/index.js:6:33)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/app.js:7:14)
at Module._compile (module.js:456:26)
24 Aug 11:35:07 - [nodemon] app crashed - waiting for file changes before starting...
This is part of my app.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/fanOcesa');
var Schema = mongoose.Schema;
var ObjectID = Schema.ObjectID;
var Teewt = new Schema({
cuerpo: String
});
var Teewt = mongoose.model('Teewt', Teewt);
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
And this is part of my index.js
var Teewt = require('mongoose').model('Teewt');
Teewt.find({}, function(err, docs){
console.log('docs');
console.log(docs);
});
exports.index = function(req, res){
res.render('index', {
docs: docs
});
};
which would be the correct way to do this query?
To fix the 'schema hasn't been registered for model' error with Mongoose, we should call require on the model files we created. to create the PostSchema and register it as 'Post' with the mongoose. model method. const mongoose = require('mongoose'); mongoose.
A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
import mongoose from 'mongoose'; const Schema = mongoose. Schema; const postSchema = new Schema({ title: { type: 'String', required: true }, content: { type: 'String', required: true }, slug: { type: 'String', required: true } }); let Post = mongoose. model('Post', postSchema);
The index.js
file is executed where your app.js
file calls:
var routes = require('./routes');
So be sure that's being called after your calls to register the 'Teewt'
schema as a mongoose model in app.js
.
Name your schema and model differently. Re-declaring Teewt
is a javascript "bad part" as well as a mistake in any programming language. Just call the schema TeewtSchema
and the model Teewt
(since the schema is typically only used in 1 file in an application, but the model may be used extensively).
I got this error too. Was baffled for long, following this and similar threads for help, trying to debug. Ultimately the issue turned out to be due to mongoose version in my case. I was trying to use mongoose-fixture to seed some default data to start with into mongo.
Try this: delete the node_modules
dir of your project, run an npm cache clean
, and then an npm install
. If even this doesn't help, try comparing versions of mongoose
/ mongoose-fixture
between the problematic app and one that works, and try changing the version in your package.json
, and repeating the above steps. This worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With