【问题标题】:How to call static function in a static class JS?如何在静态类 JS 中调用静态函数?
【发布时间】:2022-07-01 22:52:09
【问题描述】:

我正在使用 express 框架和续集 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()

在添加和删除方法中,我执行相同的操作:解析请求。我决定将它全部带入静态方法并处理类的静态字段,但出现以下错误:

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)

我只是遵循 DRY 原则,但我不知道如何使它更正确。提前谢谢你。

【问题讨论】:

  • IMO 使用 this.deviceId 没有意义,因为它应该是静态属性。

标签: javascript node.js express sequelize.js


【解决方案1】:

【讨论】:

    【解决方案2】:

    你必须打电话

    BasketControlller.anyStaticMemberOfTheClass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多