gpt4 book ai didi

node.js - :Sequelize how set returned data order

转载 作者:搜寻专家 更新时间:2023-10-31 22:28:14 25 4
gpt4 key购买 nike

我在设置带有限制和偏移量的 findAll 查询的返回数据顺序时遇到了问题,我使用的是文档中的示例代码:order: 'group DESC' 但它抛出错误说:

Error: SQLITE_ERROR: near "group": syntax error

这是完整的函数。

A_Model.findAll({
offset:req.query.page * req.query.rows - req.query.rows,
limit :req.query.rows // TODO: Of course, I put the trailing comma here ;)
// TODO: order: 'group DESC'
})
.success(function (docs) {
response_from_server.records = count;
response_from_server.page = req.query.page;
response_from_server.total = Math.ceil(count / req.query.rows);
response_from_server.rows = [];

for (item in docs) {
response_from_server.rows.push({
id :docs[item].id,
cell:[
docs[item].group,
docs[item].description,
docs[item].path,
docs[item].value
]
});
}

// Return the gathered data.
res.json(response_from_server);
})
.error(function (error) {
logger.log('error', error);
});

提前致谢。

最佳答案

您提供给 findAll 方法的“顺序”字符串不会在 Sequelize 中进行解析,只会进行转义以防止 SQL 注入(inject)。 "group"是大多数 一些 SQL 方言中的保留关键字,因此查询无法运行。

要使其正常工作,只需将“组”列放入 ` 中,如下所示:

A_Model.findAll({
offset:req.query.page * req.query.rows - req.query.rows,
limit :req.query.rows,
order: '`group` DESC'
})

关于node.js - :Sequelize how set returned data order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13864508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com