Why is my node event callback showing up in the ev

2019-09-17 08:52发布

I am trying to pass back a url string as "data" into my eventhandler.

My request to the server is "https://172.20.10.6/index.html". The console.log below prints out "/index.html" as I expect.

switch(req.method) {
    case 'GET':

    console.log("logged from plugin-https.js: url: " + req.url);

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

     break;

Below is the eventHandler code.
The console.log(data); evaluates to "undefined". I am trying to figure out why this is. It seems like a pretty straightforward direct callback, so I am wondering why the data is not defined when it shows up in the eventhandling code.

     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');

Also, here is my callback code

var ee = new events.EventEmitter();

function dataIn(config, callback) {
 if(typeof callback === 'function') {

   ee.on("dataIn", callback);

1条回答
够拽才男人
2楼-- · 2019-09-17 09:41

It looks like you're swallowing errors.

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

That condition (err, data) will ignore the value of err. If data is undefined, it will be falsy and you won't know that you had an error. You probably want something more like this:

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

Tracing back to your emitter, you are emitting in a way that sends your data to the first argument, which should be where the error goes. So this:

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

...should be more like this:

ee.emit("dataIn", null, req.url);
查看更多
登录 后发表回答