0

I dont know what is the problem with my code.

// emitter.js
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function Loadfun(param1, param2, db){
    function __error(error, row){
        if(error){
           this.emit('error', error);
           return true;
        }
        if(row.length < 1)
           this.emit('failure');
    }
    function doSomething(){
        db.query('select something', callback);
    }
    function callback(err, result){
        if(__error(error))
            return false;
        else
            this.emit('success', result);
    }
    this.doSomething = doSomething;
};
util.inherits(Loadfun,EventEmitter);
module.exports = Loadfun;

This is the emitter function. and i am using this for some sync db works. the following is the calling function.

var emitter = require('emitter');
router('/fetch', function(req, res){
   var fetch = new emitter(param1, param2, db);
   fetch.on('failure', function(){
       console.log('error');
   });
   fetch.on('success', function(data){
       console.log(JSON.stringify(data));
   });
   fetch.doSomething();
});

this works perfectly fine without any errors. I tried logging the flow till the emiting of success but the catching of the event emitting is not getting logged.. I dont understand what is the problem.. It would be nice if someone could help.

Demolition
  • 138
  • 1
  • 1
  • 10

2 Answers2

0

2 things that I can quickly see are:

  1. You are passing an error if(__error(error)) which is not defined there.
  2. You are calling this.emit in the callback function scope and it is pointing to the db.query and not the EventEmitter

You have to bind this to your callback. Doing the following will work for you db.query('select something', callback.bind(this)); But you also have to fix your "error" mentioned in number one.

x_maras
  • 2,177
  • 1
  • 25
  • 34
  • Thanks for the reply.. the problem was with the this pointer.. And its working fine now – Demolition Sep 20 '17 at 13:47
  • Check the following link for in depth info about `this` https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/README.md#you-dont-know-js-this--object-prototypes – x_maras Sep 20 '17 at 14:25
0

In most of your code, you are using the keyword thisin the wrong context. Every function declared with the function keyword, has its own this context, so when inside __error for example, the thisyou are referring to is not the LoadFun this and so it is not the class extending the EventEmitter class therefore does not emit anything.

You can either bind your functions when calling them, or use arrow functions, or assign to another variable, example with assigning this to another variable :

function Loadfun(param1, param2, db){
    var self = this;
    function __error(error, row){
        if(error){
           self.emit('error', error);
           return true;
        }
        if(row.length < 1)
           self.emit('failure');
    }
    function doSomething(){
        db.query('select something', callback);
    }
    function callback(err, result){
        if(__error(err))
            return false;
        else
            self.emit('success', result);
    }
    self.doSomething = doSomething;
};
Francois
  • 3,050
  • 13
  • 21