node.js - Mean Stack MissingSchemaError: Schema hasn't been registered for model "User" -
i have tried many solutions given on stack overflow nothing worked in case... here code... server.js entry point of application.
process.env.node_env = process.env.node_env || 'development'; var config = require('./config/config'), mongoose = require('./config/mongoose'), passport = require('./config/passport'), express = require('./config/express'); var passport = passport(); var db = mongoose(), app = express(); app.listen(config.port); module.exports = app; console.log(process.env.node_env + ' server running @ http://localhost:' + config.port);
my config.js is
module.exports = require('./env/' + process.env.node_env + '.js');
mongoos.js
var config = require('./config'), mongoose = require('mongoose'); module.exports = function() { var db = mongoose.connect(config.db); require('../app/models/user.server.model'); return db; };
passport.js
var mongoose = require('mongoose'); var passport = require('passport'); var user = mongoose.model('user'); module.exports = function() { passport.serializeuser(function(user, done) { done(null, user.id); }); passport.deserializeuser(function(id, done) { user.findone( {_id: id}, '-password', function(err, user) { done(err, user); } ); }); require('./strategies/local.js')(); };
express.js
var config = require('./config'), express = require('express'), bodyparser = require('body-parser'), passport = require('passport'); module.exports = function() { var app = express(); var session = require('express-session'); var flash = require('connect-flash'); app.use(flash()); app.use(session({ saveuninitialized: true, resave: true, secret: 'oursupersecretcookiesecret' })); app.use(bodyparser.urlencoded({ extended: true })); //use code before route definitions app.use(passport.initialize()); app.use(passport.session()); app.use(bodyparser.json()); app.set('views', './app/views'); app.set('view engine', 'ejs'); require('../app/routes/index.server.routes.js')(app); require('../app/routes/users.server.routes.js')(app); app.use(express.static('./public')); return app; };
and modal file users.server.model.js here.
var mongoose = require('mongoose'), crypto = require('crypto'), schema = mongoose.schema; var userschema = new schema({ name: string, email: string, username: { type: string, trim: true, unique: true }, password: string, provider: string, providerid: string, providerdata: {}, todos: {}//we use in next tutorial store todos }); userschema.pre('save', function(next) { if (this.password) { var md5 = crypto.createhash('md5'); this.password = md5.update(this.password).digest('hex'); } next(); } ); userschema.methods.authenticate = function(password) { var md5 = crypto.createhash('md5'); md5 = md5.update(password).digest('hex'); return this.password === md5; }; userschema.statics.finduniqueusername = function(username, suffix, callback) { var _this = this; var possibleusername = username + (suffix || ''); _this.findone( {username: possibleusername}, function(err, user) { if (!err) { if (!user) { callback(possibleusername); } else { return _this.finduniqueusername(username, (suffix || 0) + 1, callback); } } else { callback(null); } } ); }; var user = mongoose.model('user', userschema); exports = user;
at last 2 lines have tried different approaches tried mongoose.model('user', userschema); line doesn't worked. using tutorial https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/
you loading /config/passport before model registering. need require file registers model first. this:
var config = require('./config/config'), mongoose = require('./config/mongoose'), // require file has model model = require('./path/to/users.server.model'), passport = require('./config/passport'), express = require('./config/express'); var passport = passport(); var db = mongoose(), app = express();
alternatively, can include file inside /config/passport.js before calling var user = mongoose.model('user');
Thanks for this blog keep sharing your thoughts like this...
ReplyDeleteMean Stack Training in Chennai
Mean Stack Course in Chennai