I'm working on a nodeJS project, but when I moved a file model to another folder I got this error :
throw new mongoose.Error.MissingSchemaError(name);
MissingSchemaError: Schema hasn't been registered for model "...".
Use mongoose.model(name, schema)
I did not change anything, just moved the model from a folder to another.
Can anyone help, please,
Thank you.
thank's @materik, this is the model
"use strict";
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
i18n = require("i18next"),
timestamps = require('mongoose-timestamp');
/**
* UserGroup Schema
*/
var userGroupSchema = new Schema({
name: String,
_id : String,
members : [Number],
databases : [Number],
entity : String,
_createdAt: {type: Date},
updatedAt : Date,
notes: String,
rights:{
type: Schema.Types.Mixed
}
});
userGroupSchema.plugin(timestamps);
mongoose.model('userGroup', userGroupSchema, 'UserGroup');
this is the route
"use strict";
var mongoose = require('mongoose'),
fs = require('fs'),
csv = require('csv'),
_ = require('underscore'),
gridfs = require('../controllers/gridfs'),
config = require('../../config/config');
var RhModel = mongoose.model('rh');
var ExtrafieldModel = mongoose.model('extrafields');
var EntityModel = mongoose.model('entity');
var DictModel = mongoose.model('dict');
var UserGroupModel = mongoose.model('userGroup');
module.exports = function(app, passport, auth) {
...
The mongoose.model('modelName') will only work once the model has been registered.
Registering the model simply means your model.js (code containing the model) has been evaluated.
You are getting the error as you have not "required" your model. If you are using eg a version of MEAN it knows to automatically require the all the models from the models folder. (via a script)
If you move it out of that folder you would probably need to require it manually.
eg require('pathToYourModel'); and afterwards you can use mongoose.model('modelName');