0

Suppose that I have this module:

module.exports = {
  functionA: function (err, callback, res) {
    // some data process
    callback(err, this.functionB, res)
  },
  functionB: function(){
    //some data process
  }
}

And on my main function I have got a call like:

functionA(err, functionC, res)

This is giving me undefined for callback(which is functionB) in functionC, like detailed below:

  functionC: function (err, callback, res) {
    callback(/*some parameter */) //err: callback is undefined
  }

I have tried updating functionA with :

functionA: function (err, callback, res) {
    // some data process
    callback(err, this.functionB, res)
}

But that way I get functionB is not defined.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Marcelo
  • 784
  • 3
  • 9
  • 30
  • Hmmm I believe it is not related... – Marcelo Jun 25 '19 at 18:51
  • Please posts a [mcve] which shows how your code is not working. Currently it's very hard to tell how you're calling things. For instance, you say you have a call like `functionA(err, functionC, res)` but that's not going to work, since there is no `functionA` floating around untethered as such; it has to be `this.functionA` some other reference to the object which has `functionA` defined. ditto with `functionC`. The post @Vlad274 links to is the canonical Q&A pair for the kind of `this.blah is undefined` error one usually sees. – Heretic Monkey Jun 25 '19 at 19:02
  • Well it is easy to understand that functionA is exported to main. – Marcelo Jun 25 '19 at 19:04
  • Well, if you hear someone telling you that it's not easy to understand your post, and you have people marking your post as a duplicate, you have a couple of choices at minimum: 1. Accept the duplicate and move on. 2, [Edit] your question with with code that unequivocally demonstrates the issue and shows that the duplicate does not answer your question. I leave the choice to you. – Heretic Monkey Jun 25 '19 at 19:08

1 Answers1

-1

Why would you even try to do that?

If i figured what you are up to so this is an option:

module.exports = {
  functionA: function (err, callback, res) {
    // some data process
    this.functionB(err, callback, res);
  },
  functionB: function(err, callback, res){
    //some data process
    callback(err, res)
  }
}

I Believe that your way isn't working cause you try to return a module function that is not exported, I didn't check but i think outside module export it might work.. and another thing is that you could just call it instead of bringing it back in the callback considering the fact that this is an exported function :) so your way is kind of trying to solve a problem that dose not exist.

Kashkashio
  • 487
  • 5
  • 10
  • Hmmm I believe you missed functionC, is the one that executes functionB as a callback – Marcelo Jun 25 '19 at 18:57
  • Well functionC could call directly to functionB as i said it is exported... The whole presented concept is wrong and unconventional. – Kashkashio Jun 25 '19 at 19:06