gpt4 book ai didi

node.js - 包含在 包含在 Sequelize 中

转载 作者:行者123 更新时间:2023-12-03 22:34:35 25 4
gpt4 key购买 nike

基本上我有一个名为 UserLadder 的模型,其中包含另一个名为 User 的模型:

  try {
const leaderboard = await UserLadder.findAll({
where: {
weeklyLadderId: req.params.id,
},
limit: req.params.playersNumber,
attributes: ['userPoints'],
include: [{ model: User, attributes: ['displayName', 'profilePicture'] }],
order: [['userPoints', 'DESC']],
});

res.json(leaderboard);
} catch (error) {
console.error(error);
res.status(500).send('Server Error.');
}
结果:
[
{
"userPoints": 132.5,
"user": {
"displayName": "Ervin Howell",
"profilePicture": "https://banner2.cleanpng.com/20180926/igw/kisspng-league-of-legends-esports-logo-font-game-v-v-pls173-5bab06e810b838.0162687215379350800685.jpg"
}
},
{
"userPoints": 0,
"user": {
"displayName": "Leanne Graham",
"profilePicture": "https://banner2.cleanpng.com/20180926/igw/kisspng-league-of-legends-esports-logo-font-game-v-v-pls173-5bab06e810b838.0162687215379350800685.jpg"
}
}
]
但是这个用户模型与另一个名为 RiotAccount 的模型有关系,我试图弄清楚如何将第三个模型的数据与用户一起获取,因为我在 json 响应中需要它。

最佳答案

您可以为 User 模型添加“内部”包含,如下所示:

const leaderboard = await UserLadder.findAll({
where: {
weeklyLadderId: req.params.id,
},
limit: req.params.playersNumber,
attributes: ['userPoints'],
include: [{
model: User,
include: [{
model: RiotAccount, attributes: [...]
}]
attributes: ['displayName', 'profilePicture']
}],
order: [['userPoints', 'DESC']],
});

关于node.js - 包含在 包含在 Sequelize 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62658284/

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