Search code examples
ember.jsember-dataember-cli

Get associated model's data in Ember


models/card.js

export default Model.extend({
  title: attr('string'),
  description: attr('string'),
  users: hasMany('user')
});

models/user.js

export default Model.extend({
  email: attr('string'),
  name: attr('string'),
  cards: hasMany('card')
});

app/routes/cards.js

model() {
  let user = this.store.find('user', 1); // here user is defined

  user.get('cards').then((cards) => {
    console.log(cards); // here user.get('cards') is undefined
  });
}

I want to get all the associated cards which are associated with the user.

Repo link: https://github.com/ghoshnirmalya/hub-client


Solution

  • .find() returns a promise, so you should do something like this:

    this.store.find('user', 1)
      .then(user => user.get('cards'))
      .then(cards => console.log(cards));