Ember数据 - 未加载相关记录

时间:2015-03-25 01:00:38

标签: ember.js ember-data

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

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

这是来自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
      ]
   }
}

模型/ 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')
});

模型/ author.js

import DS from 'ember-data';

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

1 个答案:

答案 0 :(得分:1)

你有没有像这样嘲笑你的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
      ]
   }
}

错误说明some of the associated records were not loaded