I'm learning the MEAN stack and I'm still fairly new. I have a small app built in Mean.io, and I'm trying to move it over to Mean.js because that platform seems to be more maintained.
Things were going well, but I hit a roadblock and I need some insight...
My core custom module that contains most of my app’s functionality depends on Mean.js’ included users module to extend the user's Express schema so I can store custom data needed for the app in the user document in MongoDB (preferences, some records, etc.), but I'm running into trouble. The app crashes when I try to run grunt, and I get the following error:
/Users/myuser/Projects/pomapp2/node_modules/mongoose/lib/index.js:335
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at Mongoose.model (/Users/myuser/Projects/pomapp2/node_modules/mongoose/lib/index.js:335:13)
at Object.<anonymous> (/Users/myuser/Projects/pomapp2/modules/pom-app/server/models/pomData.server.model.js:5:21)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at /Users/myuser/Projects/pomapp2/config/lib/mongoose.js:15:5
at Array.forEach (native)
at Object.module.exports.loadModels (/Users/myuser/Projects/pomapp2/config/lib/mongoose.js:14:30)
at Object.<anonymous> (/Users/myuser/Projects/pomapp2/config/lib/app.js:20:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/Users/myuser/Projects/pomapp2/server.js:6:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
[nodemon] app crashed - waiting for file changes before starting...
This appears to me to be a result of the modules in the modules folder being loaded in alphabetical order. My module is named "pom-app" and so gets loaded before "users" so there's no schema to extend yet, and everything blows up.
If I rename my core module folder to z-pom-app making it load last, grunt starts the Mean.js app successfully. Some of the functionality in my module is broken because Angular still registers my module name without the z resulting in requests to files in the old path, but I think that would be fixed by simply renaming the Angular module. However, the important part is that the Mean.js core runs and the Express module extension code now works, and new users that register get my additional fields added to their user document exactly as I expect, just like they do in my original functioning Mean.io app.
So to fix the problem... I’m reluctant to rename my module to z-pom-app because that seems really hacky (I don’t want that in urls) and there has to be a clean way to do it. What am I missing here? How do I get the users module to load before my module does? Or am I approaching the situation entirely wrong because I’m a newbie, in which case, how should I change my app’s structure?
I’m pretty new here like I said, so if you think I’m missing something basic, it’s quite possible. ;)
I hugely appreciate any help or thoughts you might have here, thanks!