Javascript Node.js <Class> 不是构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39432687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:33:07  来源:igfitidea点击:

Node.js <Class> is not a constructor

javascriptnode.js

提问by user994165

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

尝试使用“new”实例化该类时,出现“HttpHandlers 不是构造函数”错误。

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

正在实例化的类 (../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)

回答by jfriend00

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:

您正在分配模块的属性为.HttpHandlers,而不是分配整个模块,因此如果您想要该属性,则必须引用该属性。如果您希望它以另一种方式工作,您可以更改为:

exports = HttpHandlers;

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

然后,你require()可以像这样工作:

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

回答by Mr Benn

I got this error when calling new ClassName();and it was caused in the ClassName class by missing off the "module." from module.exports = ClassName

我在调用时遇到了这个错误new ClassName();,它是由于缺少“模块”而在 ClassName 类中引起的。从module.exports = ClassName

Just in case someone else is as daft as me...

以防万一别人和我一样愚蠢......