Fetch nested relationship data fails


#1

I cannot fetch nested relationship from the store of my ember application. The relationship is as follows:

project <=> client <=> offices.

Versions:

Ember: 2.12.0 Ember Data: v2.12.1

Here are my models:

project: import DS from ‘ember-data’;

export default DS.Model.extend({
  title: DS.attr('string'),
  client: DS.belongsTo('client')
});

client:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string') ,
  projects: DS.hasMany('project'),
  offices: DS.hasMany('office')
});

office:

import DS from 'ember-data';
export default DS.Model.extend({
  country: DS.attr('string'),
  client: DS.belongsTo('client')
});

Here is my route:

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.get('store').findRecord('project', 1);
  }

The requested “offices” are in the payload from the server and are stored in the store. In my route, my model is a project object. The client of the project has 2 offices. When I try to fetch the client, everything works like a charm. When I additionally try to fetch the client’s offices I get 0 records. This is how I try to fetch them:

let client = model.get(‘client’); client.get(‘offices’);

When the promise is resolved, there are no records.

Any ideas?


#2

I think I was running into a similar issue: EmbeddedRecordsMixin: Embedded hasMany is only visible after explicitly calling store.find() I needed to wait for the hasMany relationship to be fulfilled before I was able to get the correct data. something like: client.get('offices').then((offices) => { console.log(offices.get('length'); }); should get you going. However, you might want to take this with a grain of salt, as I am not 100% sure if this really is exactly what is going on under the hood.