王如锵
王如锵

Reputation: 991

sequelize includes create a nested object

I want to do inner join from model User to model Team. Here is the code:

User.findAll({
  arrtibute: ['name'],
  include: [{
    model: Team,
    arrtibutes: ['teamName']
  }]
})

Then,I get:

[ 
  { 
    name: 'wrq',
    team: {
      teamName: 'teamA'
    }
  },
  { 
    name: 'wyx',
    team: {
      teamName: 'teamB'
    }
  },
]

But what I expect is :

[ 
  { 
    name: 'wrq',
    teamName: 'teamA'
  },
  { 
    name: 'wyx',
    teamName: 'teamB'
  },
]

How can I do this by sequelize?

Upvotes: 1

Views: 1374

Answers (1)

user5383152
user5383152

Reputation:

You could simply map the values.

User.findAll({
    attributes: ['name'],
    include: [{
        model: Team,
        attributes: ['teamName']
    }]
}).then(function(users) {
    return users.map(function(user) {
        return {
            name: user.name,
            teamName: user.team.teamName
        }
    });
}).then(function(users) {
    // The rest of your logics here...
});

Upvotes: 3

Related Questions