Srijit
Srijit

Reputation: 505

Node.js Reference error while passing an object to a require module

I am trying to develop a Node.js web application and I am trying to delegate some initialization steps to a different module

my server.js file

var express = require('express')
var app = express();  
var appInfo = {
    port:8000,
    appName:'MyApp',
    authExceptions:[],
    addExceptions:function(exceptionString){      
      authExceptions.push(exceptionString);
    }
};
var appConfigFile = '../MyApp/config';
logMsg('Reading file:'+appConfigFile); 
require(appConfigFile)(app,appInfo);
app.listen(appInfo.port);

My config.js file is

module.exports=function(app, appInfo){
   appInfo.addExceptions('/employee/*');
}

When I run this I get the below exception

ReferenceError: authExceptions is not defined
    at Object.applicationInfo.(anonymous function).addExceptions(c:\my
projects\Server\server.js:8:7)

I am thinking that JavaScript passes object by reference, so I am wondering why this error. Please help and thanks in advance

Upvotes: 1

Views: 783

Answers (1)

robertklep
robertklep

Reputation: 203349

You're treating authExceptions as if it's a (global) variable a resolvable (free) variable in current scope, but it's not: it's a property of the appInfo object.

Try this instead:

...
addExceptions: function(exceptionString) {      
  this.authExceptions.push(exceptionString);
}

Upvotes: 2

Related Questions