D
D
decvdence2020-12-22 22:38:32
Node.js
decvdence, 2020-12-22 22:38:32

What is the difference between a normal and a static method in this case?

There are two files, one contains a class with cart methods:

class Cart {
    fetch() {}
}

In another routing:
const Cart = require('../models/cart')

router.get('/', async (req, res) => {
    const cart = await Cart.fetch()
})

Please help me figure out why the error Cart.fetch is not a function occurs, even though if you make the fetch method static, this error does not occur.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-12-22
@decvdence

Normal methods are defined only on the class instance, they are not on the class itself. To call such a method, the class must be instantiated.

const cart = new Cart()
await cart.fetch()

Static methods are defined only in the class, they are not in the instance. You don't need to instantiate the class to access these methods.
await Cart.fetch()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question