gpt4 book ai didi

node.js - 如何对 Sequelize 中的记录进行排序?

转载 作者:行者123 更新时间:2023-12-03 22:24:49 28 4
gpt4 key购买 nike

我有以下带有 Sequelize 数据查询的 API。
这工作正常,但我需要在模型买家地址簿中以“id”的递减顺序对记录进行排序。所以我使用了'order',但它对输出没有任何影响。它总是按递增顺序排列的。我怎么能修改这个?我是 Sequelize 的新手。

{ model: BuyerAddressBooks, attributes: { exclude: commonExcludes },  order: [['id', 'DESC']] },
这是我的 API 代码:
export const PG_getUserDetails = createContract("Any name as of now")  //PG_BuyerProfile#get
.params("userId")
.schema({
userId: V.number(),
})
.fn(async (userId) => {
const user = await User.findByPk(userId, {
attributes: { exclude: commonExcludes },
include: [
{ model: PG_UserDetails, attributes: { exclude: commonExcludes } },
{
model: BuyerProfile,
attributes: { exclude: commonExcludes },
include: {
model: PhoneNumbers,
attributes: ["id", "number", "mobileVerified"],
},
},
{ model: BuyerAddressBooks, attributes: { exclude: commonExcludes }, order: [['id', 'DESC']] },
],
}).then(toPlain);
if (!user) {
throw new LogError("Profile not found", 400);
}
const { buyerProfile, buyerAddressBooks, phoneNumbers } = user;
const email = R.pathOr(user, ["userDetail", "email"], null);
return { email, ...buyerProfile, buyerAddressBooks, phoneNumbers };
})
.express({
method: "get",
path: "/buyer/pg_profile",
auth: true,
async handler(req, res) {
// @ts-ignore
const user = req.user;
console.log(`From console : ${user.id}`);
res.json(await PG_getUserDetails(+user.id));
},
});

最佳答案

include 选项没有 order 属性。您可以尝试在 order 模型的选项中指示 User,如下所示:

await User.findByPk(userId, {
attributes: { exclude: commonExcludes },
order: [['BuyerAddressBooks', 'id', 'DESC']],
...

关于node.js - 如何对 Sequelize 中的记录进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65459143/

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