我正在使用 Sequelize,我想按位置值排序。我认为 Sequelize 随机给出结果。
这是我的查询:
router.post("/", (req, res) => {
const value = req.body.value
Titles.findAll({
where:
{ [Op.or]: [
{title: { [Op.iLike]: '%' + value + '%'}},
{titleLink: { [Op.iLike]: '%' + value + '%'}}
]
},
attributes: ['id', 'title', 'titleLink', 'entryCount'],
///limit: 10,
}).then(result => {
res.json({'titles': result})
})
//res.send({results: req.body})
})
它给出的结果看起来像这样
值: 测试
结果:
结果(我想要):
最佳答案
如果 Titles
有一个 position
字段,那么您可以简单地使用 the doc 中提到的 order 属性(或使用 id
字段):
router.post("/", (req, res) => {
const value = req.body.value
Titles.findAll({
where:
{ [Op.or]: [
{title: { [Op.iLike]: '%' + value + '%'}},
{titleLink: { [Op.iLike]: '%' + value + '%'}}
]
},
attributes: ['id', 'title', 'titleLink', 'entryCount'],
order: [['position', 'ASC']],
///limit: 10,
}).then(result => {
res.json({'titles': result})
})
//res.send({results: req.body})
})
关于node.js - 按值位置排序 findAll order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59515681/