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

  • SEARCH
  • Home
  • 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 8286939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:54:31+00:00 2026-06-08T11:54:31+00:00

What is the best way to require a mongoose Schema in nodejs? Originally I

  • 0

What is the best way to require a mongoose Schema in nodejs?

Originally I had these inside the app.js file but that is getting a bit large and unwieldy with more models.

Now I want to move them into a models folder and use Model = require('./models/model') to import them into app.js

How do I get it such that Model is populated with the actual model?

(exports = mongoose.model(...) fails and gives me a blank object; exports.model = mongoose.model(...) requires me to do Model.model to access it — neither of these are the desired behavior)

===

Edit1

So basically I have taken

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

var UserSchema = new Schema({
  username: String,
  password: String,
  first_name: String,
  last_name: String,
  email: String
});
User = mongoose.model('User', UserSchema);

and put it into ./models/user.js

How do I get it such that its the equivalent of having this in the app.js?

  • 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-06-08T11:54:32+00:00Added an answer on June 8, 2026 at 11:54 am

    In your app.js server file, include the model.js file like this:

    var Model = require('./models/model');  //whatever you want to call it
    

    You can then instantiate it in your server file like this:

    //Initiate the  Business API endpoints
    var model = new Model(mq, siteConf);
    model.getUser(id, function() {
        // handle result
    });
    

    —-

    Then in your file you place in models folder named model.js (or whatever you want) you can set it up like this:

    var mongoose = require('mongoose'); 
    
    //MongoDB schemas
    var Schema = mongoose.Schema;
    
    var User = new Schema({
      username: String,
      password: String,
      first_name: String,
      last_name: String,
      email: String
    });
    var UserModel = mongoose.model('User', User);
    
    // your other objects defined ...
    
    
    module.exports = function(mq, siteConf) {
    //MongoDB
    mongoose.connect(siteConf.mongoDbUrl);
    
    
    // ------------------------
    // READ API
    // ------------------------
    
    // Returns a user by ID
    function getUser(id, found) {
        console.log("find user by id: " + id);
        UserModel.findById(id, found);
    }
    
    // Returns one user matching the given criteria
    // mainly used to match against email/login during login
    function getUserByCriteria(criteria, found) {
        console.log("find user by criteria: " + JSON.stringify(criteria));
        UserModel.findOne(criteria, found);
    }
    
    
    
        // more functions for your app ...
    
    
    
    return {
        'getUser': getUser, 
                'getUserByCriteria': getUserByCriteria
       };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to run Linux commands that require su with Java?
I understand Flash CS5 isn't the best way to make an app, but I'd
My seeds.rb file is getting very large. What is the best way to refactor
When testing for exceptions with PHPUnit, what is the best way to require that
What is the best way of handling redirects in an AJAX call that require
What's the best way to implement user controls that require AJAX callbacks? I want
What's the best way to structure a Sinatra app that uses DataMapper? Make a
What's the best way to require all files from a directory in ruby ?
What is the best way to implement arbitrary row reordering in a tableview that
What is the best way to present the additional spacing that should come between

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.