简体   繁体   English

TypeError,不是 NodejS 中的 function 错误

[英]TypeError, is not a function error in NodejS

I have a class that contains few functions, putting one of then below,我有一个 class 包含几个功能,将其中一个放在下面,

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,如果我像下面这样在其他 class 中导入它,

const x = await XYZ.getData();

It is throwing me some is not a function error like this,它给我一些不是这样的 function 错误,

XYZ.getData is not a function

What is the mistake I'm making?我犯了什么错误?

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

Could work可以工作

1. You have to create a class instance first to call instance methods. 1.你必须先创建一个class实例才能调用实例方法。

try this:尝试这个:

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

2. For your case try to make it static: 2.对于您的情况,请尝试将其设为 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM