[英]TypeError: Object function…has no method 'on'
I am new to node.js and trying to create my first module. 我是node.js的新手并尝试创建我的第一个模块。 But i am getting an error
但我收到一个错误
TypeError: Object function SetAstAppLog.....has no method 'on'
My module file contains following code : 我的模块文件包含以下代码:
var EventEmitter = require('events').EventEmitter;
var util = require('util');
module.exports = SetAstAppLog;
util.inherits(SetAstAppLog, EventEmitter);
function SetAstAppLog(logFolderPath,fileNamePrefix,fileSize,logStreamObject) {
EventEmitter.call(this);
.
.
.
.
this.emit('objCreated');
}
and in app.js i am doing following things : 在app.js我正在做以下事情:
var SetAstAppLog = require('astAppLog');
var fileSize = 1024;
SetAstAppLog.on('objCreated', function () {
console.log('an object was created');
});
SetAstAppLog.on('written', function () {
console.log('Write operation completed.');
});
var objCommLogger = new SetAstAppLog(logFolderPath,logCommFilePrefix,fileSize,logCommMsg);
var objErrorLogger = new SetAstAppLog(logFolderPath,logErrorFilePrefix,fileSize,logErrorMsg);
Here, i am using node js with v0.10.21. 在这里,我使用节点js与v0.10.21。
I am not able to find out why i am getting this error even my module file contains EventEmitter
我无法找出为什么我收到此错误,即使我的模块文件包含
EventEmitter
Can anyone help to solve this issue? 任何人都可以帮忙解决这个问题吗?
You are exporting the function without calling it. 您正在导出该函数而不调用它。 You need to call it:
你需要打电话给它:
var SetAstAppLog = require('astAppLog')();
Edit: I failed to notice that you called the function lower in your script. 编辑:我没注意到你在脚本中调用了较低的函数。 You should be attaching the event handlers to the instances, not the constructor.
您应该将事件处理程序附加到实例,而不是构造函数。
var objCommLogger = new SetAstAppLog(logFolderPath, logCommFilePrefix, fileSize, logCommMsg);
objCommLogger.on(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.