Requiring model schemas in another model for mongoose in different files Requiring model schemas in another model for mongoose in different files mongodb mongodb

Requiring model schemas in another model for mongoose in different files


You can get direct access of the underlying schema from a model instance as follows:

module.exports = function (connection) {    var mongoose = require('mongoose'),        Role = require('./role.js'),        RoleSchema = mongoose.model('Role').schema,        Schema = mongoose.Schema;    var user = new mongoose.Schema({        first_name: String,        last_name: String,        email: String,        password: String,        profile_image: String,        company_name: String,        type: [{ type : String, default: 'Local' }],        created_at: { type : Date, default: Date.now },        created_by: { type: Schema.Types.ObjectId, ref: 'User' },        role: [RoleSchema]    });    return mongoose.model('User', user);}