简体   繁体   English

TypeError:对象函数...没有'on'方法

[英]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.

相关问题 未捕获的TypeError:对象函数没有方法 - Uncaught TypeError: Object function has no method 未捕获的TypeError:对象函数()没有方法项 - Uncaught TypeError: Object function () has no method items 未捕获的TypeError:对象函数()没有方法“替换” - Uncaught TypeError: Object function () has no method 'replace' 未捕获的TypeError:对象函数没有方法 - Uncaught TypeError: Object function has no method TypeError:对象函数Object(){[native code]}没有方法'method' - TypeError: Object function Object() { [native code] } has no method 'method' 未捕获的TypeError:对象[object Object]没有方法'tagsInput'(匿名函数) - Uncaught TypeError: Object [object Object] has no method 'tagsInput' (anonymous function) 未捕获的TypeError:对象函数(a){返回新j(a)}没有方法'has' - Uncaught TypeError: Object function (a){return new j(a)} has no method 'has' TypeError:对象函数Object(){[native code]}没有方法'observe' - TypeError: Object function Object() { [native code] } has no method 'observe' 未捕获的TypeError:对象[object Window]没有方法'each'函数 - Uncaught TypeError: Object [object Window] has no method 'each' function 未捕获的TypeError:对象(JS函数)没有方法“应用” - Uncaught TypeError: Object (JS Function) has no method 'apply'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM