1

I have two files in helpers folder, one is EventHelper.js and UserEvent.js,

helpers/EventHelper.js

function EventHelper() {
  this.onEventCreated = function(err, e) {...}
  this.isExisting = function(id) {...}
}
module.exports = new EventHelper();

helpers/UserEvent.js

var EventHelper1 = require('./EventHelper')
var EventHelper2 = require('./EventHelper.js')

function UserEvent() {
  this.fireEvent = function(req, res) {
    var EventHelper3 = require('./EventHelper');
    ...
    EventHelper1.onEventCreated(err, e);
    EventHelper2.onEventCreated(err, e);
    EventHepler3.onEventCreated(err, e);
  }
};
module.exports = new UserEvent();

controllers/EventController.js

 var EventHelper = require('../helpers/EventHelper')
    var UserEvent = require('../helpers/UserEvent');

    var EventController = {
      fireEvent: function(req, res) {
        if(EventHelper.isExisting(req.params.id)) UserEvent.fireEvent(req, res)
        ...
      }
    }

EventHelper1 and EventHelper2 in UserEvent.js are always empty(having {}), where as EventHelper3 is being initialised properly and able to refer object's methods properly. I have referred this EventHelper.js in different controllers folder(EventController.js) globally and it was getting populated properly. I am not sure if I am missing something here

Louis
  • 146,715
  • 28
  • 274
  • 320
Srinivas
  • 553
  • 9
  • 21
  • Is this all of your code? This is what I'd expect to see if `EventHelper.js` required `UserEvent.js` somewhere. – Aaron Dufour Dec 30 '14 at 03:38
  • Are you using `console.log(EventHelper1)`, which is when you see `{}` ? If so, I know the problem :) – m59 Dec 30 '14 at 03:39
  • 5
    I hope you know that you're always getting the **same instance** of your EventHelper. Modules are only executed *once*. – LJᛃ Dec 30 '14 at 03:40
  • @m59, yes I am checking `EventHelper1` and `EventHelper2` with `console.log(EventHelper1)` and the result is `{}`, of course if I refer `EventHelper.onEventCreated()`,I am getting error `Cannot call method 'onEventCreated' of undefined` – Srinivas Dec 30 '14 at 03:45
  • Hmm, the problem isn't immediately obvious to me then. I was thinking the problem is that you're logging an object that has properties on its prototype which wouldn't show up in the log, but would work fine in actuality. – m59 Dec 30 '14 at 03:50
  • @LJ_1102, yes I wanted it to be single instance referred across as there is no data associated with `EventHelper` only methods – Srinivas Dec 30 '14 at 03:53

1 Answers1

0

You're missing some semicolons:

function EventHelper() {
   this.onEventCreated = function(err, e) {...}
   this.isExisting = function(id) {...}
}**;**
module.exports = new EventHelper();

For instance after adding this one, I got the EventHelper within your Controller, which I haven't gotten before.

Furthermore (I guess it's just and typo here and not in your actual code, since then you couldn't get an instance for it), it's:

EventHelper3.onEventCreated(err,e);

and not `

EventHepler3.onEventCreated(err,e);

So to sum up, the following is now working for me:

// EventHelper.js
function EventHelper() {
   this.onEventCreated = function(err, e) {
      console.log('testCreate');
   };
   this.isExisting = function(id) {
      console.log('testExists' + id);
      return true;
   };
};  
module.exports = new EventHelper();

// UserEvent.js
var EventHelper1 = require('./EventHelper.js');
var EventHelper2 = require('./EventHelper.js');


function UserEvent() {
   this.fireEvent = function(req, res) {
      console.log(req + res);
      var EventHelper3 = require('./EventHelper');
      EventHelper1.onEventCreated(req, res);
      EventHelper2.onEventCreated(req, res);
      EventHelper3.onEventCreated(req, res);
   };
};
module.exports = new UserEvent();

// EventController.js
var EventHelper = require('../helpers/EventHelper.js');
var UserEvent = require('../helpers/UserEvent.js');

var EventController = {
   fireEvent: function(req, res) {
      if(EventHelper.isExisting(1)) UserEvent.fireEvent(req, res);
   };
 }

 EventController.fireEvent("1","2");

Withe the following output:

testExists1
12
testCreate
testCreate
testCreate

I hope that I could help you!

fr33g
  • 2,249
  • 1
  • 12
  • 12
  • in actual code I have proper semicolons every where. If there would have been problem, I think `EventHelper` shouldn't get populated in `EventController`. It is getting populated in `EventController` and not getting populated in `UserEvent`. – Srinivas Dec 30 '14 at 13:51
  • On my machine it is actually. Look at the output. I receive the "testCreate" three times, so .onEventCreated is called for each of the three EventHelper objects. Thus they are populated. That's weird, so it's not working for you? – fr33g Dec 30 '14 at 19:25