0
votes

I try to load a mock Json, but im getting the following error:

Uncaught Error: Assertion Failed: You looked up the 'author' relationship on a 'post' with id 2 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.belongsTo({ async: true }))

here is the JSON from http://localhost:4200/api/posts/2

{  
   "post":{  
      "id":2,
      "title":"Monkeys",
      "date":"2013-12-21T00:04:20.461Z",
      "author":1,
      "body":"Vestibulum porttitor leo maximustae ultricies risus efficitur sit amet."
   },
   "author":{  
      "id":1,
      "name":"George",
      "posts":[  
         2
      ]
   }
}

models/post.js

import DS from 'ember-data';

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

models/author.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    posts: DS.hasMany('post')
});
1
Shouldn't posts be side loaded with something like this "posts": [ {"id": 1, "title": "one"}, {"id": 2, "title": "two"} ... ]maxhungry

1 Answers

1
votes

Have you try mocking your json like this?

{  
   "posts":[
       {  
          "id":2,
          "title":"Monkeys",
          "date":"2013-12-21T00:04:20.461Z",
          "author":1,
          "body":"Vestibulum porttitor leo maximustae ultricies risus efficitur sit amet."
       }
   ],
   "author":{  
      "id":1,
      "name":"George",
      "posts":[  
         2
      ]
   }
}

The error states some of the associated records were not loaded.