mongoose: MissingSchemaError: Schema hasn't been registered for model
Error:
/var/www/html/noodetest/node_modules/mongoose/lib/index.js:329
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at Mongoose.model (/var/www/html/noodetest/node_modules/mongoose/lib/index.js:329:13)
at Object.<anonymous> (/var/www/html/noodetest/app/models/rrss.js:3:29)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/var/www/html/noodetest/server.js:36:14)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Model user:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Social = mongoose.model('Social');
var userSchema = new mongoose.Schema({
name: { type: Schema.Types.String, required: true },
nick: { type: Schema.Types.String, required: true },
bio: { type: Schema.Types.String, required: true },
avatar: { type: Schema.Types.String, required: false },
rrss: [
{
type: Schema.Types.ObjectId, ref: 'Social'
}
]
});
module.exports = mongoose.model('User', userSchema);
Model social:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
User = mongoose.model('User');
var socialSchema = new mongoose.Schema({
twitter: { type: Schema.Types.String },
github: { type: Schema.Types.String },
web: { type: Schema.Types.String },
user_id: [
{
type: Schema.Types.String, ref: 'User'
}
]
});
module.exports = mongoose.model('Social', socialSchema);
// Load models
var Social = require('./app/models/rrss'),
User = require('./app/models/user'),
Schema = mongoose.Schema;
What i miss?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 18
You called
require()on the social schema first, which attempts to compile the ‘Social’ model that references a ‘User’ model that isn’t defined until later. Since you have circular refs, there’s no way for mongoose to successfully compile these models, which is fine since circular refs are often indicative of poor schema design choices.