Tejas Mehta
Tejas Mehta

Reputation: 301

Javascript - 'x' is not a function

In my javascript file I am calling function B inside function A. I get the following error

B is not a function.

How can I resolve this error?

exports.createRecord = function A() {
  B();

};

exports.B = () =>{
}

Upvotes: 0

Views: 30

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370729

Assigning a property to an object does not put that property's name into scope as a standalone identifier. For similar reasons, the following will fail too:

const obj = {
  fn() {
    console.log('hi');
  }
};
fn();

And module.exports is just an object with the same sort of behavior.

Either do

exports.createRecord = function A() {
  exports.B();
};

or

exports.createRecord = function A() {
  B();
};

const B = () => {
};
exports.B = B;

Upvotes: 1

Related Questions