Brandon Palmer
Brandon Palmer

Reputation: 313

Node.JS - Cannot read property 'ColorType' of undefined

I'm having a little issue with "Enums" in Node.js at the moment.

I currently have a file called 'colorHandler.js' in my lib/ folder, and I'm accessing it in another file in my /lib. the format in chatHandler.js is as followed:

var format = {
    GREEN: {irc: '\u000309', mc: 'a'},
    RED: {irc: '\u000304', mc: 'c'},
}

And in my IRC Handler, I currently have just one declaration (after requiring, of course) that looks lik this:

Command.test = function(ocmd) {
    client.say(to, c.format.GREEN.irc. + from + ': '+ c.format.RED.irc +'Command handler works!');
}

The error I am recieving in my debugger is: "message": "uncaughtException: Cannot read property 'GREEN' of undefined"

What is going on here, it use to work for me, now it's just constantly throwing the error once the 'test' command is typed in chat.

Upvotes: 0

Views: 443

Answers (2)

Thibault Sottiaux
Thibault Sottiaux

Reputation: 197

You need to export your object with module.exports.format = format.

When writing a node module, only the attributes defined on module.exports or exports will be exposed. You can then require the module and access the exposed attributes in another node module.

Your code should look like

var format = {
    GREEN: {irc: '\u000309', mc: 'a'},
    RED: {irc: '\u000304', mc: 'c'},
};

module.exports.format = format;

Upvotes: 0

Anoop
Anoop

Reputation: 23208

Change var format to module.exports.format

module.exports.format = {
    GREEN: {irc: '\u000309', mc: 'a'},
    RED: {irc: '\u000304', mc: 'c'},
}

when you use require(...) it become

(function (exports, require, module, __filename, __dirname) {
  module.exports.format = {
    GREEN: {irc: '\u000309', mc: 'a'},
    RED: {irc: '\u000304', mc: 'c'},
  }
});

Upvotes: 1

Related Questions