为什么我的节点事件回调显示为未定义的事件

Why is my node event callback showing up in the event as undefined?

本文关键字:事件 未定义 显示 回调 我的 节点 为什么      更新时间:2023-09-26

我正试图将url字符串作为"数据"传递回我的eventandler。

我对服务器的请求是"https://172.20.10.6/index.html"。下面的console.log如我所料打印出"/index.html"。

switch(req.method) {
    case 'GET':
    console.log("logged from plugin-https.js: url: " + req.url);
     ee.emit("dataIn", req.url);
     break;

下面是eventandler代码。
console.log(data);的计算结果为"undefined"。我想知道为什么会这样。这似乎是一个非常简单的直接回调,所以我想知道为什么数据在eventhandhandling代码中出现时没有定义。

     plugin = rtrn;
         console.log('4');
         plugin.dataIn(config, function(err, data) {
           if(err, data) {
             console.log('error geting data');
           } else {
            // This is where request events are being handled.  
            console.log(data);

            console.log('We are in the event handling codeblock of funtion startRuntime() located in interactor.js');

还有,这是我的回调代码

var ee = new events.EventEmitter();
function dataIn(config, callback) {
 if(typeof callback === 'function') {
   ee.on("dataIn", callback);

看起来你在吞错误。

if(err, data) {
    console.log('error geting data');
}

条件(err, data)将忽略err的值。如果data是未定义的,它将是假的,你不会知道你有一个错误。您可能想要更像这样的内容:

if (err) {
    console.log('error getting data')
}

追溯到你的发射器,你发射的方式发送你的数据到第一个参数,这应该是错误的地方。所以这:

ee.emit("dataIn", req.url);

…应该更像这样:

ee.emit("dataIn", null, req.url);