这是我从数据库中获取节目及其相关场地和乐队的查询。 我真的只想得到乐队和场地的名称。(name
是这两个表中的字段。不过,下面的代码正在获取整个记录,而不仅仅是我想要的字段。
const getAllShows = async (req, res) => {
try {
const shows = await Show.findAll({
include: [
{ model: User, as: 'bands', through: { attributes: ['name'] } },
{ model: Venue, through: { attributes: ['name'] }}
],
});
res.status(200).send(shows);
}
catch(err) {
res.send(400);
}
}
attributes
放错了地方 - 它不属于through
(顺便说一句,根据您的关联,您甚至可能不需要through
(。
尝试像这样更改:
{ model: User, as: 'bands', attributes: ['name']},
您还可以考虑字段别名,如下所示:
{ model: User, as: 'bands', attributes: [['name', 'band_name']]},
呵呵