我通过使用Express编写的REST API将帖子(标题,文本)保存到mongodb数据库并刷新浏览器后出现此错误.我已经将主键设置为'_id',并且已经阅读了有关可能正常化数据的内容?
这是服务器的有效负载(db中只有1个帖子):
{
"posts": [
{
"title": "The Title",
"text": "Lorem ipsum",
"_id": "52c22892381e452d1d000010",
"__v": 0
}
]
}
控制器:
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(); } } });
该模型:
App.Post = DS.Model.extend({ title: DS.attr('string'), text: DS.attr('string') });
串行:
App.MySerializer = DS.RESTSerializer.extend({ primaryKey: function(type){ return '_id'; } });
适配器:
App.ApplicationAdapter = DS.RESTAdapter.extend({ namespace: 'api' });
任何帮助深表感谢!如果您需要任何其他信息,请告诉我.谢谢
使用自定义适配器/序列化器时,命名很重要.如果您希望它应用于整个应用程序,则应调用它ApplicationSerializer
App.ApplicationSerializer = DS.RESTSerializer.extend({ primaryKey: '_id' });
适配器:
App.ApplicationAdapter = DS.RESTAdapter.extend({ namespace: 'api' });
如果您只想将它应用于单个模型(这也适用于适配器)
App.PostSerializer = DS.RESTSerializer.extend({ primaryKey: '_id' });