服务器排序对我不起作用。我使用ORM Sequelize。默认情况下,我必须按名称和升序进行排序。但当我想在客户端上按降序排序时,什么也没发生,但这样的请求会显示在终端中。
执行(默认):SELECT count(*)AS";计数";来自"spr_turbodrills";AS";spr_turbodrills";;
正在执行(默认):SELECT";turbodrill_id"name"心轴"涡轮钻_ n";,"createdAt"updatedAt";FROM";spr_turbodrills";AS";spr_turbodrills";ORDER BY";spr_turbodrills"name";ASC限制1偏移0;
也就是说,根本没有发生基于sql查询的排序。
虽然如果你看看这里,一切似乎都很好。
GET/api/directory/spr_turbodrills/allorder=desc&pageSize=1&第1200页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)
}
}
我认为这是因为您在控制器中硬编码为始终使用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