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 8845023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:38:09+00:00 2026-06-14T11:38:09+00:00

I have a problem to give a name to a controller to load my

  • 0

I have a problem to give a name to a controller to load my models:

my app.js

var express = require('express');
var app = express();
app.everyauth = require('everyauth');
app.mongoose = require('mongoose');

var fs = require('fs');

var config = require('./config.js')(app, express);

//Include models
var models = {};
fs.readdir(__dirname + '/models', function(err, files){
    if (err) throw err;
    files.forEach(function(file){
        var name = file.replace('.js', '');
        models.name = require('./models/' + name)(app.mongoose).model;
    });
});

//Include controllers
fs.readdir(__dirname + '/controllers', function(err, files){
    if (err) throw err;
    files.forEach(function(file){
        var name = file.replace('.js', '');
        require('./controllers/' + name)(app, models);
    });
});

app.listen(process.env.PORT || 3000);

for exemple I have test.js in models

If I want to find something in my db I would like to do:

models.test.find({}, function(err, docs){});

but it doesn’t works because test is unknow, I should do

models.name.find({}, function(err, docs){});

So I would replace name by the name of the model

Thanks

  • 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-14T11:38:10+00:00Added an answer on June 14, 2026 at 11:38 am

    Try this;

    models[name] = require('./models/' + name)(app.mongoose).model;
    

    If you want to read directory sync mode, you can use

    var models = {};
    var files = fs.readdirSync(__dirname + '/models');
    for(var i in files) {
      var name = files[i].replace('.js', '');
      models[name] = require('./models/' + name)(app.mongoose).model;
    }
    
    // Now, you can use
    models.test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem, please give me some suggestion ? My problem is :
I have a problem with BlackBerry JDE 6.0 The following code give me the
i will give an example of the problem i have. My XML is like
Hope someone will give me a hand with this problem I have. So here
I have web-service which give list of property at a particular area My problem
I have problem with UIWebView delay when the load image from url. In my
I have problem with validation a very specific beans. Let me give you some
I have a problem with FULL JOIN keyword used in MySQL SELECT Name,Surname,Notes.Payment FROM
I have a problem. I have a label which displays a name. When I
I have a problem with this code, maybe you can give me a hand.

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.