node.js - 包含在包含在续集中
问题描述
基本上我有一个名为 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']],
});
推荐阅读
- amazon-web-services - 如何在 CI/CD 上为 AWS Chalice 应用程序安全地设置环境变量?
- database - 如何在 MongoDB 中查询数组?
- vue.js - 如果填写了所有必填字段,如何更改按钮颜色
- php - 如何调整 Bootstrap 进度条的宽度并充分利用进度条?
- python - Pandas 中的 Student Uplift 数据提取
- amazon-web-services - 如果参数为空列表,AWS AppSync vtl make set null
- python - 有没有办法缩短多个 python 数组语句?
- java - 使用二分查找查找两个排序数组的所有公共元素
- php - 如何从分类的 PHP 文件中调用非分类 PHP 文件中的函数,每个函数在不同的命名空间中?
- android - 为什么实时数据在竞争条件下没有观察到一些价值?