Question

I am trying to create models in mongoose using an express app. the error i am getting is the following:

Did you try nesting Schemas? You can only nest using refs or arrays.

My Setup is the following:

  1. I have a module names Schemas which I am linking to my Express app using npm link
  2. The Schemas module has an entry point index.js which has the following

    module.exports = require('./lib/index');
    
  3. The above required index file(./lib/index.js) has:

    module.exports = {    InstituteSchema: require('./Schemas/Institute') }
    
  4. The above Institute file(./Schemas/Institute) has the following:

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    
    var RegistrySchema = new Schema({
    name: String,
    privileges: [String]
    });
    
    module.exports = RegistrySchema;
    
  5. The following is an extract of my express app:

    var express = require('express');
    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/Project');
    var http = require('http');
    var path = require('path');
    var routes = require('./routes');
    var RegistrySchema = require('Schemas').RegistrySchema;
    var RegistryModel = mongoose.model('Registry', RegistrySchema);
    

however when I run Express i get the following stack trace:

   /usr/local/bin/node app.js

   /Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:362 throw new TypeError('Undefined type at `' + path +

   TypeError: Undefined type at `paths.name`
   Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:362:11)
at Schema.path (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:305:29)
at Schema.add (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:217:12)
at Schema.add (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:212:14)
at new Schema (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/schema.js:73:10)
at Mongoose.model (/Users/user/Documents/WebstormProjects/Project/node_modules/mongoose/lib/index.js:293:14)
at Object.<anonymous> (/Users/user/Documents/WebstormProjects/Project/app.js:12:30)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)

Process finished with exit code 8
Was it helpful?

Solution

I would suggest you have a models directory within which you have a file defined for each model. In the model file export the model itself.

In models/Registry.js -

module.exports = mongoose.model('Registry', RegistrySchema);

And in your app.js, just require all the model files -

var fs = require('fs'),
    modelPath = __dirname + '/models';

fs.readdirSync(modelPath).forEach(function(file) {
    require(modelPath + '/' + file);
});

Have a look at the MEAN stack boilerplace structure - https://github.com/linnovate/mean/tree/master/app

Also have a look at this article to understand how require works - http://openmymind.net/2012/2/3/Node-Require-and-Exports/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top