(Node.js/Express.js) Error: Schema hasn't been registered for model "document"
I was trying to work my way through the nodepad tutorial on Dailyjs.com (found here). I eventually came to halt when I received this upon trying to execute(after step 2 of tut):
Error: Schema hasn't been registered for model "Document".
Use Mongoose.define(name, schema)
at Mongoose.model (/usr/local/lib/node/.npm/mongoose/1.0.16/package/lib/mongoose/index.js:138:13)
at Object.<anonymous> (/root/Repos/nodepad/models.js:3:10)
at Module._compile (module.js:374:26)
at Object..js (module.js:380:10)
at Module.load (module.js:306:31)
at Function._load (module.js:272:10)
at require (module.js:318:19)
at Object.<anonymous> (/root/Repos/nodepad/app.js:10:16)
at Module._compile (module.js:374:26)
at Object..js (module.js:380:10)
Mostly I'm a complete noob here, so what I really need is not so much a "do this 开发者_JAVA技巧to make it work" answer, but if you don't mind, an explanation as to what the actual cause of this error is. I can post the actual code I have now if needed, and apologize if this is some agonizingly simple issue.
models.js:
var mongoose = require('mongoose');
mongoose.model('Document', {
properties: ['title', 'data', 'tags'],
indexes: ['title']
});
exports.Document = function(db) {
return db.model('Document');
};
Since Mongoose 1.0 you have to define your models in a different way. To see that applied to nodepad, i recommend reading up on the source of nodepad (especially the file models.js
).
Example:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var User = new Schema({
email: {
type: String,
index: { unique: true }
},
name: String,
lastseen: Date,
isonline: Boolean,
hashed_password: String,
salt: String
});
mongoose.model('User', User);
Sadly, the 'older' blogposts of the (absolutely awesome) DailyJS tutorial are kind of outdated. Alex Youg did post some follow-up posts to migrate the existing tutorial to Mongoose 1.0 (which introduced really heavy API changes), see here.
精彩评论