0

server.js

require('babel/register');
require('./index');

index.js

'use strict';

import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import path from 'path';
import mongoose from 'mongoose';

import { MONGO_CONFIG } from './config';
import { Routes } from './api/routes';
import { RequestLogger } from './lib';

const app = new express();
const requestLogger = new RequestLogger(FILE_CONFIG.LOG_DIR);

app.use(cors());
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({
    limit: "50mb",
    extended: true,
    parameterLimit: 50000
    })
);
app.use(requestLogger.init());

Routes.init(app);

app.use("/static/js/", express.static("./public/js"));
app.use("/static/html/", express.static("./public/html"));
app.use("/static/images/", express.static("./public/images"));
app.use("/static/css/", express.static("./public/css"));
app.use("/static/fonts/", express.static("./public/fonts"));

const PORT = process.env.PORT || 8080;

app.listen(PORT, function() {
    console.log("[Info] Listening on Port:", PORT);
    console.log("[Info] Connecting to MongoDB...");

    // MongoDB Connection
    mongoose.Promise = global.Promise;
    mongoose.connect(MONGO_CONFIG.CONNECTION_URI,MONGO_CONFIG.OPTIONS).then(
    function() {
        console.log('[Info] MongoDB Connection to Database "' + MONGO_CONFIG.DBNAME + '" Successful!');
    },
    function(error) {
        console.error("[Error] MongoDB Connection Error:", error);
        return process.exit(-1);
    }
    );
});

User.js (Model)

"use strict";

import async from 'async';
import { MongoModel, ResponseBody } from '../../lib'; 
import UserSchema from '../schemas';
import { Aegis } from '../../lib';

const aegis = new Aegis();
const mongoModel = new MongoModel('User', UserSchema);

export const UserModel = {
  create: create,
  index: mongoModel.index,
  findById: mongoModel.findById,
  login: login,
  getSecretKey: getSecretKey
}

MongoModel.js

'use strict';

import mongoose from 'mongoose';
import async from 'async';

export class MongoModel {
  constructor(modelName, Schema) {
    this.MongooseModel  = mongoose.model(modelName, Schema);
    this._queryParams   = Schema._queryParams;
    this._filterQuery   = Schema._filterQuery;

    // Method Hard-Binding
    this._execQuery       = this._execQuery.bind(this);
    this._findRaw         = this._findRaw.bind(this);
    this._findOneRaw      = this._findOneRaw.bind(this);
    this._find            = this._find.bind(this);
    this._findOne         = this._findOne.bind(this);
    this.index            = this.index.bind(this);
    this.findById         = this.findById.bind(this);
    this.create           = this.create.bind(this);
    this.update           = this.update.bind(this);
    //this.update_or_insert = this.update.bind(this);
    this.findOneAndUpdate = this.findOneAndUpdate.bind(this);
    this.remove           = this.remove.bind(this);
  }

I am getting following error:

MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at new MissingSchemaError (D:\PersonalProjects\cyborg\node_modules\mongoose\lib\error\missingSchema.js:20:11)
at Mongoose.model (D:\PersonalProjects\cyborg\node_modules\mongoose\lib\index.js:391:13)
at new MongoModel (D:/PersonalProjects/cyborg/lib/MongoModel.js:11:36)
at Object.<anonymous> (D:/PersonalProjects/cyborg/api/models/User.js:9:20)
at Module._compile (module.js:635:30)
at normalLoader (D:\PersonalProjects\cyborg\node_modules\babel\node_modules\babel-core\lib\api\register\node.js:199:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\PersonalProjects\cyborg\node_modules\babel\node_modules\babel-core\lib\api\register\node.js:216:7)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (D:\PersonalProjects\cyborg\api\models\index.js:11:13)
at Module._compile (module.js:635:30)
at normalLoader (D:\PersonalProjects\cyborg\node_modules\babel\node_modules\babel-core\lib\api\register\node.js:199:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\PersonalProjects\cyborg\node_modules\babel\node_modules\babel-core\lib\api\register\node.js:216:7)

I have seen many similar questions posted on stackoverflow but couldn't find a solution. Please Help.

The code was working perfectly fine, but i started getting this error after i made few changes to migrate from ES5 to ES6.

Have referred following links

MissingSchemaError: Schema hasn't been registered for model "User"

Mongoose Schema hasn't been registered for model

a--m
  • 4,716
  • 1
  • 39
  • 59
ankit
  • 1,499
  • 5
  • 29
  • 46

1 Answers1

1

Try to change your import file to:

import { UserSchema } from '../schemas';

(Trying to do some guessing answer here since I cannot see you '../schemas' file.)

a--m
  • 4,716
  • 1
  • 39
  • 59