javascript - Class.method is not a function using ES6 classes - Stack Overflow

I create a class and I want to use the method in routes in my node.js application.A simple snippet cod

I create a class and I want to use the method in routes in my node.js application. A simple snippet code describe a smilar issue that I faced in my real app.

myClass.js:

class MyClass {
    static async sayHi(name) {
        setTimeout(() => {
            return `Hello ${name}`
        }, 3000)
    }
}

module.exports = new MyClass()

index.js:

const MyClass = require('./myClass');
console.log(MyClass.sayHi)

I got this error:

undefined

Trying:

console.log(MyClass.sayHi('Hello'))

returns:

MyClass.sayHi is not a function

I create a class and I want to use the method in routes in my node.js application. A simple snippet code describe a smilar issue that I faced in my real app.

myClass.js:

class MyClass {
    static async sayHi(name) {
        setTimeout(() => {
            return `Hello ${name}`
        }, 3000)
    }
}

module.exports = new MyClass()

index.js:

const MyClass = require('./myClass');
console.log(MyClass.sayHi)

I got this error:

undefined

Trying:

console.log(MyClass.sayHi('Hello'))

returns:

MyClass.sayHi is not a function
Share Improve this question edited Jun 13, 2019 at 10:09 Slim asked Jun 13, 2019 at 9:59 SlimSlim 6,19714 gold badges53 silver badges95 bronze badges 2
  • Where do you test console.log(MyClass.sayHi('Hello'))? you never export the class therefore you would never be able to call this code externally (FWIW this is exactly how you should be calling the function as it's static). – James Commented Jun 13, 2019 at 10:03
  • Don't export module.exports = new MyClass(), export the MyClass itself! – Bergi Commented Jun 13, 2019 at 10:03
Add a ment  | 

4 Answers 4

Reset to default 6

You're exporting an instance of the class - when importing, you have an instance, not the actual class. Calling the imported object MyClass is confusing - maybe call it myClassInstance instead, which would make it clear what the problem is.

If you have an instance, and want to call a static method on the class, reference the .constructor property.

If you want to return something asynchronously, best to use a Promise, and then call .then on the sayHi call to consume the Promise:

class MyClass {
  static sayHi(name) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(`Hello ${name}`);
      }, 1000);
    });
  }
}

const myClassInstance = new MyClass();
// module.exports = myClassInstance;

// index.js:

// const myClassInstance = require('./index');
myClassInstance.constructor.sayHi('foo')
  .then(console.log);

No need for the static method to be async, since you're already returning a Promise explicitly.

You are exporting an instance of MyClass. Static methods are not called on instances of the class, but rather on the class itself.

If you export MyClass like so: module.exports = MyClass, your second approach should work.

edit:

As @T.J. Crowder pointed out. If the class is in a file called MyClass.js, import from there instead of index.

@Murtaza Hussain is right.

As the method is static. you can just export the class instead creating an instance.

module.exports = MyClass;

Change line from instance to Class itself, as Static methods are the methods that can be called without creating an instance of class.

module.exports = MyClass;

and change the require to the file name

 const MyClass = require('./myClass.js');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743783886a4506302.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信