gpt4 book ai didi

javascript - 无法连接 eventEmitter 上的监听器

转载 作者:行者123 更新时间:2023-12-03 09:37:44 26 4
gpt4 key购买 nike

我正在尝试使用 Node 的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);

关于javascript - 无法连接 eventEmitter 上的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31279924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com