简体   繁体   English

如何在静态类 JS 中调用静态函数?

[英]How to call static function in a static class JS?

Im working in express framework and sequelize orm, and i have following controller for the basket of devices:我在 express 框架和 sequelize orm 中工作,并且我有以下用于一篮子设备的控制器:

const {Basket, BasketDevice, Device} = require('../models/models')
const ApiError = require('../error/ApiError')

class BasketControlller {
    static deviceId;
    static quantity;
    static basket;
    static basketDevice;


    async getRequestValues(req){
        const {deviceId} = req.body
        let {quantity} = req.body
        this.deviceId = deviceId
        this.quantity = quantity
        
        this.basket = await Basket.findOne({
            where: {userId: req.user.id},
        })
        this.basketDevice = await BasketDevice.findOne({
            where: {deviceId: deviceId, basketId: basket.id},
        })
        this.BasketControlller.deviceId = deviceId
        return this
    }

    async add(req, res, next){
        this.getRequestValues(req)

        if(this.basketDevice){
            if(!this.quantity){
                this.quantity = 1
            }

            this.basketDevice = await BasketDevice.update(
                {quantity: this.basketDevice.quantity + Number(this.quantity)}, 
                {where: {deviceId: this.deviceId, basketId: this.basket.id}
            })
        }
        else{
            this.basketDevice = await BasketDevice.create({deviceId: this.deviceId, basketId: this.basket.id})
        }
        
        return res.json({message: `Successfully added ${this.quantity} units of goods`})
    }

    async getOne(req, res, next){
        const basket = await Basket.findOne({
            where: {userId: req.user.id},
            include: {
                model: BasketDevice,
                include: {
                    model: Device
                },
                attributes: ['id', 'deviceId', 'basketId', 'quantity'],
            }, 
        })    

        return res.json(basket)
    }

    async delete(req, res, next){
        this.getRequestValues()

        if(this.basketDevice){
            if(!this.quantity){
                this.quantity = 1
            }
            else if(this.basketDevice.quantity == 1){
                this.basketDevice = await BasketDevice.destroy({
                    where: {
                        deviceId: this.deviceId,
                    }
                })
                return res.json({message: 'The product was successfully deleted'})
            }
            this.basketDevice = await BasketDevice.update(
                {quantity: this.basketDevice.quantity - Number(this.quantity)}, 
                {where: {deviceId: this.deviceId, basketId: this.basket.id}
            })
        }
        else{
            return next(ApiError.badRequest('The product in the basket is already missing'))
        }
        
        return res.json({message: `Successful deleted ${quantity} units of goods`})
    }
}

module.exports = new BasketControlller()

In a add and delete methods i do the same operations: parsing request.在添加和删除方法中,我执行相同的操作:解析请求。 I decided to do bring it all into the static method and working through static fields of class, but i get following error:我决定将它全部带入静态方法并通过类的静态字段工作,但我收到以下错误:

D:\JavaScript\testNodeReact\server\controllers\basketController.js:30
    this.getRequestValues(req)
         ^

TypeError: Cannot read properties of undefined (reading 'getRequestValues')
    at add (D:\JavaScript\testNodeReact\server\controllers\basketController.js:30:14)
    at Layer.handle [as handle_request] 
(D:\JavaScript\testNodeReact\server\node_modules\express\lib\router\layer.js:95:5)

I just followed DRY principe, but i dont know how make it more correct.我只是遵循 DRY 原则,但我不知道如何使它更正确。 Thank you in advance.先感谢您。

你必须打电话

BasketControlller.anyStaticMemberOfTheClass

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

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