Ember.js - Error - “Assertion failed:您必须在传递给”push“的散列中包含一个”id“ [英] Ember.js - Error - "Assertion failed: You must include an `id` in a hash passed to `push`"

查看:101
本文介绍了Ember.js - Error - “Assertion failed:您必须在传递给”push“的散列中包含一个”id“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是服务器的有效载荷(只有1个帖子,以db为单位):

_id:52c22892381e452d1d000010,
__v:0
}
]
}
/ pre>

控制器:

  App.PostsController = Ember。 ArrayController.extend({
actions:{
createPost:function(){
// Dummy content for now
var to_post = this.store.createRecord('post',{
标题:'标题',
文本:'Lorem ipsum'
});
to_post.save();
}
}
});

模型:

  App.Post = DS.Model.extend({
title:DS.attr('string'),
text:DS.attr('string')
});

序列化器:

 code> App.MySerializer = DS.RESTSerializer.extend({
primaryKey:function(type){
return'_id';
}
});

适配器:

 code> App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace:'api'
});

任何帮助都非常感谢!如果您需要任何其他信息,请通知我。
感谢

解决方案

使用自定义适配器/序列化器时,命名很重要。如果要将其应用于整个应用程序,应该称为 ApplicationSerializer

  App.ApplicationSerializer = DS.RESTSerializer.extend({
primaryKey:'_id'
});

适配器:

 code> App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace:'api'
});

如果您只想将其应用于单个模型(这适用于适配器) p>

  App.PostSerializer = DS.RESTSerializer.extend({
primaryKey:'_id'
});


I'm getting this error after I save a post (title, text) to a mongodb database via a REST API written with Express and refresh the browser. I've already set my primary key to '_id' and have been reading about possibly normalizing the data?

Here is the payload from the server (only 1 post in db):

{
  "posts": [
  {
    "title": "The Title",
    "text": "Lorem ipsum",
    "_id": "52c22892381e452d1d000010",
    "__v": 0
   }
  ]
}

The controller:

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

The model:

App.Post = DS.Model.extend({
   title: DS.attr('string'),
   text: DS.attr('string')
});

Serializer:

App.MySerializer = DS.RESTSerializer.extend({
  primaryKey: function(type){
    return '_id';
  }
});

Adapter:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api'
});

Any help is much appreciated! Please let me know if you need any other info. Thanks

解决方案

When using custom adapters/serializers the naming is important. If you want it to apply to the entire application it should be called ApplicationSerializer

App.ApplicationSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

Adapter:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api'
});

If you just want it to apply to a single model (this applies to adapter's as well)

App.PostSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

这篇关于Ember.js - Error - “Assertion failed:您必须在传递给”push“的散列中包含一个”id“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆