Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7504959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:44:53+00:00 2026-05-29T21:44:53+00:00

I currently have all my models (Schema definitions) in the /models/models.js file for my

  • 0

I currently have all my models (Schema definitions) in the /models/models.js file for my Mongoose/NodeJS application.

I’d like to break these apart into different files as such: user_account.js, profile.js, etc. However I cannot seem to do so as my controllers break and report back “cannot find module” once I pull these classes apart.

My project structure is as follows:

/MyProject
  /controllers
    user.js
    foo.js
    bar.js
    // ... etc, etc
  /models
    models.js
  server.js

The contents of my models.js file looks like this:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

mongoose.connect('mongodb://localhost/mydb');

var UserAccount = new Schema({
    user_name       : { type: String, required: true, lowercase: true, trim: true, index: { unique: true } }, 
    password        : { type: String, required: true },
    date_created    : { type: Date, required: true, default: Date.now }
}); 

var Product = new Schema({
    upc             : { type: String, required: true, index: { unique: true } },
    description     : { type: String, trim: true },
    size_weight     : { type: String, trim: true }
});

My user.js file (controller) looks like this:

var mongoose    = require('mongoose'), 
    UserAccount = mongoose.model('user_account', UserAccount);

exports.create = function(req, res, next) {

    var username = req.body.username; 
    var password = req.body.password;

    // Do epic sh...what?! :)
}

How can I break the schema definition into multiple files and also reference it from my controller? When I do reference it (after the schema is in a new file) I get this error:

*Error: Schema hasn’t been registered for model “user_account”.*

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:45:01+00:00Added an answer on May 29, 2026 at 9:45 pm

    Here’s a sample app/models/item.js

    var mongoose = require("mongoose");
    
    var ItemSchema = new mongoose.Schema({
      name: {
        type: String,
        index: true
      },
      equipped: Boolean,
      owner_id: {
        type: mongoose.Schema.Types.ObjectId,
        index: true
      },
      room_id: {
        type: mongoose.Schema.Types.ObjectId,
        index: true
      }
    });
    
    var Item = mongoose.model('Item', ItemSchema);
    
    module.exports = {
      Item: Item
    }
    

    To load this from an item controller in app/controllers/items.js I would do

      var Item = require("../models/item").Item;
      //Now you can do Item.find, Item.update, etc
    

    In other words, define both the schema and the model in your model module and then export just the model. Load your model modules into your controller modules using relative require paths.

    To make the connection, handle that early in your server startup code (server.js or whatever). Usually you’ll want to read the connection parameters either from a configuration file or from environment variables and default to development mode localhost if no configuration is provided.

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need my application run in multiple screen resolutions. Currently I have specified all
I currently have a bunch of repositories like so IMyRepository IAnotherRepository They all inherit
I currently have an iPhone application with a tabbar and multiple viewcontrollers. All the
Currently I have a UITabBarController with 4 items on it that all have associated
We currently have an error catching script for all our PHP sites. This script
I currently have a issue with firefox, where all other browser behave in the
I currently have a table called paper , which holds all the information for
Hello I currently have this code checking if a cookie exists, it works all
Currently i have a build process in place for all of our apps using
Backgroup: We are looking at SAS BI Dashboard. We have currently implemented almost all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.