1

I have a class that contains few functions, putting one of then below,

const _ = require("lodash");
const ObjectId = require("mongoose").Types.ObjectId;
const mockLookUps = require(“../../data”).mockLookUps;

class XYZ {
   // ...... Other functions
   async getData() {
    return Promise.resolve(mockLookUps); // JSON Object
  }
}

module.exports = XYZ;

if I import that in other class like below,

const x = await XYZ.getData();

It is throwing me some is not a function error like this,

XYZ.getData is not a function

What is the mistake I'm making?

3

2 Answers 2

1
const xyz = new XYZ()
const data = await xyz.getData()

Could work

Sign up to request clarification or add additional context in comments.

Comments

0

1. You have to create a class instance first to call instance methods.

try this:

const xyz = new XYZ();
const result = await xyz.getData();

2. For your case try to make it static:

class XYZ {
   // ...... Other functions
   static async getData() {
    return Promise.resolve(mockLookUps); // JSON Object
  }
}

module.exports = XYZ;

and then you can use so:

const x = await XYZ.getData();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.