【发布时间】:2021-04-04 03:13:57
【问题描述】:
我有以下带有Sequelize 数据查询的API。
这工作正常,但我需要在模型 BuyerAddressBooks 中按“id”的递减顺序对记录进行排序。所以我使用了“订单”,但它对输出没有任何影响。它总是按增量顺序排列。我怎么能修改这个?我是 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));
},
});
【问题讨论】:
标签: node.js sequelize.js