【问题标题】:sequelize Mysql data two between date all record show but some ... findall REST API ......simple problemssequelize Mysql data two between date all record show but some ... findall REST API ......简单问题
【发布时间】:2022-01-20 07:38:51
【问题描述】:

这是我的 API 代码。它运行良好,但缺少我的“startedDate”:“2022-01-01”,并且 "endDate":"2022-01-31" ...我如何编写函数请解决我的问题并分享你的想法..

  async getcurrentmonthorder2(req, res, next) {
        try {
    
            const {startedDate ,endDate} = req.body;
            
            db.product.findAll({
                attributes:[[sequelize.fn('sum',sequelize.col('profit_total')),'total']],
             
                where : {"createdAt" : {[Op.between] : [new Date(startedDate).setHours(0,0,0,0) , new Date(endDate).setHours(23,59,59,999) ]}},
               
             
            })
    
        
          
       
            .then(list => {
                res.status(200).json({ 'success': true, data: list });
               
            })
            .catch(function (err) {
                next(err)
            });
    }
    catch (err) {
        res.status(500).json({ 'errors': "" + err });
    }

}

#我已经从邮递员那里得到了输出..

{
    "success": true,
    "data": [
        {
            "total": "121",
            
        }
    ]
}

#but 我想要这个输出请帮忙分享你的想法

 {
        "success": true,
        "data": [
            {
                "total": "121",
                "startedDate":"2022-01-01",
                "endDate":"2022-01-31"
            }
        ]
    }

我的数据

{id: 1, total: 20, profit_total: 15, createdAt: "2021-12-30T15:59:10.000Z",…}

【问题讨论】:

    标签: node.js reactjs express sequelize.js


    【解决方案1】:

    您的问题来自您在查询中指出的属性:您告诉 SEQUELIZE 只返回 TOTAL。我建议你这样做::

    db.product.findAll({
       attributes:[[sequelize.fn('sum',sequelize.col('profit_total')),'total'], 'startedDate', 'endDate'],
       where : {
         "createdAt" : {
            [Op.between] : [new Date(startedDate).setHours(0,0,0,0), new Date(endDate).setHours(23,59,59,999) ]}
       },
    })
    

    【讨论】:

    • 抱歉不起作用...显示错误 ["startedDate"] 不是有效的属性定义。请使用以下格式:['attribute definition', 'alias'] 错误:未指定默认引擎且未提供扩展名。
    • 这是我的数据库 {id: 1, total: 20, profit_total: 15, createdAt: "2021-12-30T15:59:10.000Z",…}
    • 它对我有用,我忘了删除“startedDate”和“endDate”上的[]。我更新了我的解决方案。谢谢
    • 你能分享你的api代码吗...?
    • 请查看我的数据库...
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多