Skip to content
Advertisement

sequelize include returns only one result

I have a Users table and a Groups table, when I retrieve a single user, I want to get a list of all the groups that user belongs to. I created a joins table named GroupUsers and it has userId and groupId on it, this is how I know that a user belongs to a group. Now, when I try to get the list of groups a user belongs to using the sequelize include on find, it only returns the first match, if the user belongs to various groups. How do I solve this?

Users controller

JavaScript

Returns this:

JavaScript

Instead of this:

JavaScript

I’m sure I’m doing something wrong somewhere I just don’t know where, that same thing may also be the cause of sequelize including the joins table in the result: GroupUsers

Advertisement

Answer

It appears that in my associations, I did:

JavaScript

Instead of :

JavaScript

Note the foreignKey attribute

And as for the GroupUsers object that is also returned, I removed that by doing:

JavaScript

Note the through key which has attributes set to an empty array.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement