1

Routes.js includes the following lines of code:

X = {};
X.xFunction(user) { 
  // some code here 
  // console.log(user.Name);
}       
var Router_Functions = require('/Router_Functions');

app.get('/', Router_Functions.aFunction)

Router_Functions.js

exports.afunction = function (req, res) {

   xFunction(req.session.user);

}

Here, error is xFunction is undefined. But in such case, how do you to pass function X.xFunction() from 'routes.js' to 'Router_Functions.js'

Sangram Singh
  • 7,161
  • 15
  • 50
  • 79

2 Answers2

2

You have something wrong in your architecture. Such an approach is kinda wrong. If you need a function defined in one module to exists in another then you have to export it in separate file. I.e.:

// xFunction module
var X = {};
X.xFunction(user) { 
  // some code here 
  // console.log(user.Name);
}
module.exports = X;

Then in *Router_Functions.js*

exports.afunction = function (req, res) {

   var X= require("xFunction.js");
   X.xFunction(req.session.user);

}

If you really want to define the function in Routes.js then you have to pass it somehow. For example as a parameter of aFunction

app.get('/', function(req, res, next) {
   Router_Functions.aFunction(req, res, xFunction);
});

// Router_Functions.js
exports.aFunction = function (req, res, xFunction) {
   xFunction(req.session.user);
}
Krasimir
  • 13,306
  • 3
  • 40
  • 55
-1

you have to pass xFunction while calling aFunction like Router_Functions.aFunction(X.xFunction(user))