• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

231
Views
TypeError, is not a function error in NodejS

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?

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

Could work

almost 3 years ago · Juan Pablo Isaza Report

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();
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error