我尝试使用节点的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
事件
答案 0 :(得分:1)
实际上,从不调用下面的函数,因此您不会将任何侦听器附加到事件发射器。
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);