【问题标题】:Server side sorting does not occur不发生服务器端排序
【发布时间】:2023-03-11 09:30:01
【问题描述】:

服务器排序对我不起作用。我使用 ORM 续集。默认情况下,我必须按名称和升序排序。但是当我想在客户端进行降序排序时,什么都没有发生,但是终端显示了这样一个请求。

执行(默认):SELECT count(*) AS "count" FROM "spr_turbodrills" AS "spr_turbodrills";

执行(默认): 选择“turbodrill_id”、“名称”、“主轴”、“turbodrill_n”、 “createdAt”、“updatedAt” FROM “spr_turbodrills” AS “spr_turbodrills” 按“spr_turbodrills”订购。“名称” ASC 限制 1 偏移 0;

即根本没有发生基于sql查询的排序。

虽然如果你看这里,一切似乎都很好。

GET /api/directory/spr_turbodrills/all?order=desc&pageSize=1&page=1 200 7.805 毫秒 - 163

控制器:

module.exports.getAllPaginate = async function (req, res) {
    try {
        const query = {
            offset:  +req.query.pageSize * ( +req.query.page - 1),
            limit:  +req.query.pageSize,
            order: [
                ['name', 'ASC']
            ]
        }
    
        const spr_turbodrills = await SprTurbodrills.findAndCountAll(query)
        res.status(200).json(spr_turbodrills)
    } catch(e) {
        errorHandler(res, e)
    }   
}

【问题讨论】:

  • 在你controller 你不处理订单参数。你使用显式 orderby ['name', 'ASC']
  • 我觉得您发表评论时甚至没有考虑我在评论中指出的内容。甚至@feiiiiii 也根据我所说的添加了一个答案。

标签: node.js express sequelize.js


【解决方案1】:

我认为这是因为您硬编码为始终在控制器中使用 ASC

module.exports.getAllPaginate = async function (req, res) {
try {
    const query = {
        offset:  +req.query.pageSize * ( +req.query.page - 1),
        limit:  +req.query.pageSize,
        order: [
            ['name', req.query.order]
        ]
    }

        const spr_turbodrills = await SprTurbodrills.findAndCountAll(query)
        res.status(200).json(spr_turbodrills)
    } catch(e) {
        errorHandler(res, e)
    }   
}

然后试试这个:

GET /api/directory/spr_turbodrills/all?order=DESC&pageSize=1&page=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2014-10-01
    • 2010-09-13
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多