无法在 eventEmitter 上连接监听器
Not able to hook up listener on eventEmitter
我正在尝试使用节点的 events
模块设置电子邮件通知。我无法让它工作,因为它在发出 email
事件时没有做。不知道怎么了。你能帮我调试这段代码吗?提前致谢!
email.js
var express = require('express');
var nodemailer = require('nodemailer');
var events = require('events');
var util = require('util');
var _ = require('lodash');
var Notify = function() {
events.EventEmitter.call(this);
var transporter = nodemailer.createTransport();
this.eventNames = {
newBug: 'new bug',
updatedBug: 'updated bug',
commentedOnBug: 'commented on bug',
updatedAndCommentedOnBug: 'updated and commented on bug',
};
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
this.notify = function(eventName, data) {
console.log('notify...');
this.emit('email', data);
};
this.send = function(eventName, data) {
var email = {
from: 'new_bugtrack@example.com',
to: '',
subject: '',
text: ''
};
email.to = data.assignTo.email || '';
switch (eventName) {
case this.eventNames.newBug:
email.subject = 'Created new bug ' + data.id
email.text = data.title
break;
case this.eventNames.updatedBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' updated bug-' + data.id
email.text = data._.last(data.changeHistory).change
break;
case this.eventNames.commentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' commented on bug-' + data.id
email.text = data._.last(data.changeHistory).comment
break;
case this.eventNames.updatedAndCommentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + 'updated and commented on bug-' + data.id
email.text = _.last(data.changeHistory).change + '-----' + _.last(data.changeHistory).comment
break;
default:
break;
}
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
email.to = 'example@example.com';
}
console.log('email emit activated');
transporter.sendMail(email);
};
};
util.inherits(Notify, events.EventEmitter);
// dont't send emails in dev/test env
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
Notify.send = function(eventName, data) {
console.log('emailed data', JSON.stringify(data, null, 2));
};
}
module.exports.Notify = Notify;
bug.controller.js
var marklogic = require('marklogic');
var conn = require('../../config/db-config.js').connection;
var db = marklogic.createDatabaseClient(conn);
var Notify = require('../../email/email').Notify;
var email = new Notify();
console.log('notify', email);
// just a simple function for demo
exports.count = function(req, res) {
db.documents.query(
q.where(
q.collection('bugs')
)
.slice(1, 1)
.withOptions({
debug: true,
categories: 'metadata'
})
).result(function(response) {
email.notify(email.eventNames.newBug, {a: 2})
res.status(200).json({
count: response[0].total
});
});
};
...
...
...
我看到日志语句 notify
是从 notify()
函数打印的,而不是 email notifier
,它挂接到 email
事件
事实上,下面的函数永远不会被调用,所以您没有将任何侦听器附加到事件发射器。
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
修改如下:
var notifier = function(data) {
console.log('email notifier......');
};
this.on('email', notifier);
我正在尝试使用节点的 events
模块设置电子邮件通知。我无法让它工作,因为它在发出 email
事件时没有做。不知道怎么了。你能帮我调试这段代码吗?提前致谢!
email.js
var express = require('express');
var nodemailer = require('nodemailer');
var events = require('events');
var util = require('util');
var _ = require('lodash');
var Notify = function() {
events.EventEmitter.call(this);
var transporter = nodemailer.createTransport();
this.eventNames = {
newBug: 'new bug',
updatedBug: 'updated bug',
commentedOnBug: 'commented on bug',
updatedAndCommentedOnBug: 'updated and commented on bug',
};
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
this.notify = function(eventName, data) {
console.log('notify...');
this.emit('email', data);
};
this.send = function(eventName, data) {
var email = {
from: 'new_bugtrack@example.com',
to: '',
subject: '',
text: ''
};
email.to = data.assignTo.email || '';
switch (eventName) {
case this.eventNames.newBug:
email.subject = 'Created new bug ' + data.id
email.text = data.title
break;
case this.eventNames.updatedBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' updated bug-' + data.id
email.text = data._.last(data.changeHistory).change
break;
case this.eventNames.commentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' commented on bug-' + data.id
email.text = data._.last(data.changeHistory).comment
break;
case this.eventNames.updatedAndCommentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + 'updated and commented on bug-' + data.id
email.text = _.last(data.changeHistory).change + '-----' + _.last(data.changeHistory).comment
break;
default:
break;
}
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
email.to = 'example@example.com';
}
console.log('email emit activated');
transporter.sendMail(email);
};
};
util.inherits(Notify, events.EventEmitter);
// dont't send emails in dev/test env
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
Notify.send = function(eventName, data) {
console.log('emailed data', JSON.stringify(data, null, 2));
};
}
module.exports.Notify = Notify;
bug.controller.js
var marklogic = require('marklogic');
var conn = require('../../config/db-config.js').connection;
var db = marklogic.createDatabaseClient(conn);
var Notify = require('../../email/email').Notify;
var email = new Notify();
console.log('notify', email);
// just a simple function for demo
exports.count = function(req, res) {
db.documents.query(
q.where(
q.collection('bugs')
)
.slice(1, 1)
.withOptions({
debug: true,
categories: 'metadata'
})
).result(function(response) {
email.notify(email.eventNames.newBug, {a: 2})
res.status(200).json({
count: response[0].total
});
});
};
...
...
...
我看到日志语句 notify
是从 notify()
函数打印的,而不是 email notifier
,它挂接到 email
事件
事实上,下面的函数永远不会被调用,所以您没有将任何侦听器附加到事件发射器。
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
修改如下:
var notifier = function(data) {
console.log('email notifier......');
};
this.on('email', notifier);