0

I have this code in an Ember route:

let forum = this.store.findRecord('forum', 'main').then(function(forum) {
  console.log('FORUM:', forum);
  let categories = forum.get('categories');
  console.log('CATEGORIES:', categories);
});

return forum;

This console returns this:

FORUM: Object { store: Object, _internalModel: Object, id: Getter, currentState: Object, isError: false, adapterError: null, OWNER [id=__ember1476462923058714220863537]: Object, _super: ROOT(), 2 more… }

CATEGORIES: undefined

Obvisouly, a request is made to /forums/main, but nothing after that. What is the role of forum.get('categories')? How am I supposed to get the relationships of a resource? Shouldn't that request /forums/main/categories?

Forum:

export default DS.Model.extend({
  // Attributes
  language: DS.attr('string'),

  // Relationships
  categories: DS.hasMany('category', { async: true }),
});

Category:

export default DS.Model.extend({
  // Attributes
  name: DS.attr('string'),

  // Relationships
  forum: DS.belongsTo('forum', { async: true }),
  boards: DS.hasMany('board', { async: true })
});

Router (relevant part):

this.route('community', function() {
  this.route('players');
  this.route('forum', function() {
  });
});
Marc-François
  • 3,900
  • 3
  • 28
  • 47
  • There was more complex [question](http://stackoverflow.com/questions/40032747/ember-2-filter-relationship-models-hasmany-belongsto-and-calculte-computed-p) on similar topic yesterday. You can try to check it out. – Keo Oct 14 '16 at 17:04
  • We'd also need your model definition in order to properly help you here. Mainly we'll need to know how your forum-categories relationship is defined. – Jean-Philippe Roy Oct 14 '16 at 18:32
  • I edited my question. – Marc-François Oct 14 '16 at 18:38
  • You're assigning `forum` to a promise that returns nothing. This will also not work if the record returns by `.findRecord('forum', 'main')` does not have an id of `main`, you will get duplicated records because Ember Data thinks it's a new record. – locks Oct 16 '16 at 13:20
  • The request will only go to `/forums/main/categories` if you include a relationship link in your server API payload. – locks Oct 16 '16 at 13:27
  • @locks There is a relationship link in my API payload. – Marc-François Oct 18 '16 at 08:53

1 Answers1

1

In order to fetch categories as you make a request to the forum (synchronous requests), the relationship between the forum and the category model needs to be correctly stablished:

models/forum.js

App.Forum = DS.Model.extend({
  categories: DS.hasMany('categories');
});

models/category.js

App.Category = DS.Model.extend({
  forum: DS.belongsTo('forum', { async: false });
});

You may also want to get the categories once your promise is resolved. In this case, you can do something like this:

this.store.findRecord('forum', 'main').then((function(_this) {
  return function(forum) {
    console.log(JSON.stringify('forum: ' + forum));
    let categories = _this.store.find('category', forum.get('category_id'));
  };
})(this));

If you can, I encourage you to add more details (router.js, models/forum, models/categories files would help) about your scenario so I can try to to provide a more accurate answer. Ember version details can also help.

Meanwhile, this discussion and this guide may be helpful.

biker856
  • 327
  • 1
  • 4
  • 9