0

MyFunc1.js :

function MyFunc1(){
  this.client = new myClient();
  var scheduleService = new ScheduleService();
  scheduleService.updatesometing(this.funcinsideFunc1);

}

MyFunc1.prototype.funcinsideFunc1 = async function() {
  var self = this;
  return new Promise((resolve, reject) => {
    self.client.call() // error at this line Cannot read property 'call' of undefined
  }
}

ScheduleService.js :

ScheduleService.prototype.updateSomething = async function(func1){
  var result = await Promise.resolve(func1());
}

The above code produces the following error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'call' of undefined

on the line as specified in the comment in MyFunc1.js

What is the correct way to pass and execute this function?

hong4rc
  • 3,999
  • 4
  • 21
  • 40
kosta
  • 4,302
  • 10
  • 50
  • 104
  • `this.client`, created via `new`, will be an *instantiation* - that is, an object, not a function. Without seeing its code, it's impossible to tell. – CertainPerformance Sep 13 '18 at 04:59
  • funcinsideFunc1 is passed as a callback. `this` isn't class instance inside of it. – Estus Flask Sep 13 '18 at 05:01
  • You're already using an arrow function which keeps the outer `this` in `funcinsideFunc1`, you don't need to use any `self` here. Your problem is with the `updatesometing(this.funcinsideFunc1);` call – Bergi Sep 13 '18 at 08:17

1 Answers1

-1

I think I solved the problem with this:

scheduleService.updatesometing.bind(this)();

And then inside ScheduleService like the following:

this.funcinsideFunc1()
kosta
  • 4,302
  • 10
  • 50
  • 104