Skip to content Skip to sidebar Skip to footer

Ember 3.5 Belongsto Relationships Not Resolving

i used to have emberjs 1.13 which i upgraded to 3.5 i got an issue with belongsTo relationships where i cannot access those data in it. my code as below model export default DS.Mo

Solution 1:

That's a big upgrade from 1.13 straight to 3.5.

A lot has changed. To fully understand what changed and why, I much recommend reading each of the Ember release notes every time you upgrade a minor or major version. Super helpful.

Most likely author was not loaded into the store. Check your route's model(), network request and Ember Inspector if data loaded in.

If loaded in, it may be the async: true. Try remove it?

This is a working example in Ember 3.5:

app/models/thing.js:

importModelfrom'ember-data/model';
import attr from'ember-data/attr';
import { belongsTo } from'ember-data/relationships';

exportdefaultModel.extend({

  // Attributestitle: attr('string'),
  description: attr('string'),
  published: attr('string'),
  publishedAt: attr('string'),

  // Relationships:// No need for async: truecourse: belongsTo('course'),
  author: belongsTo('author')

});

app/models/author.js:

importModelfrom'ember-data/model';
import attr from'ember-data/attr';

exportdefaultModel.extend({
  name: attr('string')
});

app/route/thing.js

importRoutefrom'@ember/routing/route';

exportdefaultRoute.extend({
  model(params) {
    // Assuming you use JSON API// Make sure `author` is included when fetching `thing`returnthis.store.query('thing', {
      include: 'author, course'
    }),
  }
});

app/controllers/thing.js

importControllerfrom'@ember/controller';

exportdefaultController.extend({
  init(){
    console.log(this.model.author.name)
  }
});

Post a Comment for "Ember 3.5 Belongsto Relationships Not Resolving"