Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  170] [ 6]  / answers: 1 / hits: 71230  / 8 Years ago, sun, september 11, 2016, 12:00:00

I'm getting HttpHandlers is not a constructor error when trying to instantiate that class using new.



Class being instantiated (../lib/restifyHandlers/HttpHandlers):



var config = require('config');
module.exports.config = config;

var util = require('util');
var _ = require('underscore');
var EventEmitter = require(events).EventEmitter;

var HttpHandlers = function(eventHandlers) {
var _self = this;
this.name = HttpHandlers;
if (!(this instanceof HttpHandlers)) {
return new HttpHandlers(eventHandlers);
}
}

util.inherits(HttpHandlers, EventEmitter);

HttpHandlers.prototype.extractHttpHandlersRequest = function(req, res, next) {
var _self = this;
req.locals = {};
res.locals = {};

}
module.exports.HttpHandlers = HttpHandlers;


Code making the call:



var HttpHandlers = require('../lib/restifyHandlers/HttpHandlers');
var obj = new HttpHandlers(oneRouteConfig.eventHandlers);


Stacktrace:



2016-09-10T23:44:41.571-04:00 - [31merror[39m: Sun, 11 Sep 2016 03:44:41 GMT Worker #master: exiting from error:  TypeError: HttpHandlers is not a constructor 
TypeError: HttpHandlers is not a constructor
at setupRestifyRoute (/usr/apps/das/src/myrepo/nodejs/myapp/lib/router.js:78:14)
at Router.setup_routes (/usr/apps/das/src/myrepo/nodejs/myapp/lib/router.js:40:3)
at /usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:222:14
at initialize (/usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:38:9)
at setup_server (/usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:107:4)
at /usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:275:4
at /usr/apps/das/src/myrepo/nodejs/myapp/node_modules/temp/lib/temp.js:231:7
at FSReqWrap.oncomplete (fs.js:123:15)

More From » node.js

 Answers
6

When you assigned this:



exports.HttpHandlers = HttpHandlers;


You would need to match that with this:



var HttpHandlers = require('../lib/restifyHandlers/HttpHandlers').HttpHandlers;





You are assigning a property of your module to be .HttpHandlers, not assigning the whole module so if you want that property, you have to reference the property. If you want it to work the other way, you could change to this:



exports = HttpHandlers;


And, then your require() could work the way you are doing it like this:



var HttpHandlers = require('../lib/restifyHandlers/HttpHandlers');

[#60754] Wednesday, September 7, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarod

Total Points: 62
Total Questions: 111
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
jarod questions
;