0

Trying to create multiple factories in Node. Do they have to be in separate files? If they are, how do I make sure to access both?

index.js

var myFunc = function () {
    this.data = {
        thingOne: null,
        thingTwo: null,
        thingThree: null
    };
    this.fill = function (info) {
        for (var prop in this.data) {
            if (this.data[prop] !== 'undefined') {
                this.data[prop] = info[prop];
            }
        }
    };
    this.triggerAction = function () {
        //make some action happen!
    };
    module.exports = function (info) {
        var instance = new myFunc();
        instance.fill(info);
        return instance;
    };

When I add another function below that it breaks the existing code with an object [object Object] has no method 'triggerAction:'

var myFunc2 = function () {
    this.data = {
        thingOne: null,
        thingTwo: null,
        thingThree: null
    };
    this.fill = function (info) {
        for (var prop in this.data) {
            if (this.data[prop] !== 'undefined') {
                this.data[prop] = info[prop];
            }
        }
    };
    this.triggerAction2 = function () {
        //make some action happen!
    };
};
module.exports = function (info) {
    var instance = new myFunc2();
    instance.fill(info);
    return instance;
};

So do I have to put the second function in a separate file? And if I do, how do I modify package.json to make sure it sees the second file? Thanks!

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Raydot
  • 1,458
  • 1
  • 23
  • 38
  • Why don't you add that method in the `myFunc`? You seem to be duplicating a lot of code. – Bergi Jun 03 '14 at 21:52
  • Just cut and pasted to come up with an example. Of course I wouldn't really code that way. – Raydot Jun 04 '14 at 00:06
  • So those are totally unrelated factories? Then put them in two different modules. – Bergi Jun 04 '14 at 00:19
  • That's what I'm trying to do. So does that mean two different files or two different exports. DC5 seems on the case... – Raydot Jun 04 '14 at 16:26

1 Answers1

2

The short answer is no.

The error you are seeing is caused because you are overwriting the value of the exports property of the module - effectively replacing the first assignment with the last.

If you want these to be in the same module, you would need to export them separately:

module.exports.factoryA = function(...) {...}
module.exports.factoryB = function(...) {...}

To reference these from another module either of these patterns would work:

var factories = require('./myfactories');

var factoryAResult = factories.factoryA(...);
var factoryBResult = factories.factoryB(...);

or

var factoryA = require('./myfactories').factoryA;

var factoryAResult = factoryA(...);
dc5
  • 12,341
  • 2
  • 35
  • 47
  • How do I reference these from the app then? Require would be the same but then... – Raydot Jun 04 '14 at 16:26
  • Maybe I'm changing the original question slightly in my confusion but what's the difference between module.exports and creating custom functions like exports.create? – Raydot Jun 04 '14 at 16:45
  • 1
    Thinking of it as a plain object may be more helpful. An object can only have one property with a given key. exports is a property of the module object. Its initial value is an empty object. You can either replace the exports property as you were doing in the code provided above: `module.exports = function() {}` or you can assign properties to it: `module.exports.fn1 = function() {}`. Given that this is JS you could also assign a new object to it: `module.exports = { fn1: function() {}, fn2: function() }`. These can, in turn, be accessed by name when requiring from another module. – dc5 Jun 04 '14 at 17:03
  • Now I got it. Also: http://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-nodejs Thanks dc5! – Raydot Jun 04 '14 at 17:04