不会发生服务器端排序
服务器排序对我不起作用。我使用ORM Sequelize。默认情况下,我必须按名称和升序排序。但是当我想在客户端上按降序排序时,没有任何反应,但是这样的请求会显示在终端中。
执行(默认):SELECT count(*)AS“count”FROM“spr_turbodrills”AS“spr_turbodrills”;
执行(默认):SELECT“turbodrill_id”,“name”,“spindle”,“turbodrill_n”,“createdAt”,“updatedAt”FROM“spr_turbodrills”AS“spr_turbodrills”ORDER BY“spr_turbodrills”。“name”ASC LIMIT 1 OFFSET 0;
也就是说,基于sql查询的排序根本没有发生。
虽然如果你看这里,那么一切似乎都很好。
GET / api / directory / spr_turbodrills / all?order = desc&pageSize = 1&page = 1 200 7.805 ms - 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)
}
}
回答如下:
我认为这是因为你硬编码总是在你的控制器中使用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