小编son*_*wee的帖子

Ember.js - 错误 - "断言失败:你必须在传递给`push`的哈希中包含一个`id`"

我通过使用Express编写的REST API将帖子(标题,文本)保存到mongodb数据库并刷新浏览器后出现此错误.我已经将主键设置为'_id',并且已经阅读了有关可能正常化数据的内容?

这是服务器的有效负载(db中只有1个帖子):

{
  "posts": [
  {
    "title": "The Title",
    "text": "Lorem ipsum",
    "_id": "52c22892381e452d1d000010",
    "__v": 0
   }
  ]
}
Run Code Online (Sandbox Code Playgroud)

控制器:

App.PostsController = Ember.ArrayController.extend({
    actions: {
      createPost: function() {
        // Dummy content for now
        var to_post = this.store.createRecord('post', {
          title: 'The Title',
          text: 'Lorem ipsum'
        });
        to_post.save();
      }
    } 
 });
Run Code Online (Sandbox Code Playgroud)

该模型:

App.Post = DS.Model.extend({
   title: DS.attr('string'),
   text: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)

串行:

App.MySerializer = DS.RESTSerializer.extend({
  primaryKey: function(type){
    return '_id';
  }
});
Run Code Online (Sandbox Code Playgroud)

适配器:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api'
});
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢!如果您需要任何其他信息,请告诉我.谢谢

ember.js

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

ember.js ×1