Ember数据 - 未加载相关记录

时间:2021-10-05 09:32:33

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

我尝试加载一个模拟Json,但我得到以下错误:

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 }))

未捕获错误:断言失败:您在ID为2的“帖子”上查找了“作者”关系,但未加载某些相关记录。确保它们与父记录一起加载,或者指定关系是异步的(DS.belongsTo({async:true}))

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

这是来自http:// localhost:4200 / api / posts / 2的JSON

{  
   "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

车型/ 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

车型/ author.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    posts: DS.hasMany('post')
});

1 个解决方案

#1


1  

Have you try mocking your json like this?

你有没有像这样嘲笑你的json?

{  
   "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.

该错误表明未加载某些相关记录。

#1


1  

Have you try mocking your json like this?

你有没有像这样嘲笑你的json?

{  
   "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.

该错误表明未加载某些相关记录。