0

app.js file:

var express = require('express');
var path = require('path');
var mongoose = require('mongoose');
var bodyparser = require('body-parser');


var conn = mongoose.createConnection('mongodb://localhost/database_name');

var app = express();
app.set('view engine','ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyparser.urlencoded({extended:true}));

app.use(require('./routes/index'));
app.use(require('./routes/page1'));
app.use(require('./routes/page2'));
app.use(require('./routes/page3'));
app.use(require('./routes/page4'));

exports.conn = conn;

app.listen(3000,function(request, response){
    console.log("Server is running at Port 3000");
});

models.js file:

var mongoose = require('mongoose');
var app = require('./app');

var conn = app.conn

module.exports.User = conn.model('User', new mongoose.Schema({
    username: String,
    password: String,
}));

On running node app.js, i am getting an error that TypeError: Cannot read property 'model' of undefined. app.js and models.js are in the same folder.

P.S. i am using createConnection function because i have to make two database connections.

3
  • What is the conn you are requiring in the models.js? Do you try to use the variable conn from the app.js like this? This is not how node.js works. require('conn') will look into node_modules for a package named conn. Commented Feb 8, 2016 at 12:12
  • oh God...yes...sorry for that...my bad...but still the problem persist. conn is the mongodb connection created by createConnection function. I want to use this variable (conn) in another file(models.js) Commented Feb 8, 2016 at 12:26
  • @migg: i have modified the code for exporting variable. Please check. Commented Feb 8, 2016 at 12:42

2 Answers 2

1

You should not require app.js inside a module. That defeats the purpose. Either use a separate module that handles the connection, or use dependency injection like this:

models.js

var mongoose = require('mongoose');

module.exports = function(conn) { // inject connection
    return {
        User: conn.model('User', new mongoose.Schema({
            username: String,
            password: String,
        })
    };
}

app.js

var conn = mongoose.createConnection('mongodb://localhost/database_name');

var models = require('./models')(conn);

models.User(...) // do stuff with User function

This is better structured, more maintainable and better testable as you can inject a mock for conn.

Sign up to request clarification or add additional context in comments.

Comments

0

finally figured it out:

to make it globally available we need to replace following line

var conn = mongoose.createConnection('mongodb://localhost/database_name');

with

module.exports.conn = mongoose.createConnection('mongodb://localhost/database_name');

2 Comments

Yes you can do that but you should not.
why not? i can put connection code in a separate file and not in app.js?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.