ember-data-1.0.0 activemodeladapter错误包括传递给“push”的散列中的“id” [英] ember-data-1.0.0 activemodeladapter error include an `id` in a hash passed to `push`
问题描述
断言失败:你必须在传递给`push`的哈希中包含一个`id`
在一个jsfiddle
我已经重现了这个 的jsfiddle 即可。当我使用 id 代替时,您可以看到不同版本的 jsfiddle 工作的 _id 。
后端发送的有效负载
ActiveModel适配器将snake_case start_date 转换为camel-case。我已经将一些自定义代码添加到ember-data rest-serializer中,以将 _id 转换为 id ,并将代码粘贴到此问题中。
{schedule:[
{
_id:529f38596170700787000000 ,
name:发型,
start_date:2013-12-12
}
]}
现在,尽管返回的有效载荷包含 id ,如果我进入google cgrome控制台并运行下面的commmands并访问 _data ,它显示 id 未定义。
a = App .__ container __。lookup(controller:schedule)
b = a.get('content')
我使用控制台中的公开箭头,深入了解 _dat 对象,这是我看到的
_data:Object
id:undefined
自定义序列化程序代码
我扩展了activemodeladapter将mongodb的_id转换为id,并将其设置为主要k安永。
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
primaryKey:'_id',
normalize:function(type,hash,prop){
this.normalizeMongoidId(hash);
return this._super(type,hash,prop);
},
normalizeMongoidId:function(hash){
var json = {id:hash._id};
delete hash._id;
//hash.id = hash._id;
// delete hash._id;
}
/ *
serialize:function(record,options){
var json = this._super(record,options);
json.schedule._id = json.record.id;
delete json.record.id
return json;
}
* /
});
ember数据模型:
App.Schedule = DS.Model.extend({
name:DS.attr('string'),
organization:DS。 belongsTo('organization')
});
这是我的emberjs路由器
App.Router.map(function(){
this.resource('schedule',{path:/ schedule},function()
this.resource('schedule',{path:/:schedule_id},function(){});
});
});
emberjs路线定位:
App.SchedulesRoute = Ember.Route.extend({
model:function(){
return this.store.find('schedule' );
}
});
emberjs堆栈跟踪
断言失败:您必须在传递给`push` application.js的哈希中包含一个`id`:31601
(匿名函数)application.js:31601
Ember.assert application.js:28431
DS.Store.Ember.Object.extend.push application.js:69415
(匿名函数)application.js:69483
Ember。 EnumerableUtils.map application.js:30115
DS.Store.Ember.Object.extend.pushMany application.js:69482
(匿名函数)application.js:69839
invokeCallback application.js: 36783
(匿名函数)application.js:36838
EventTarget.trigger application.js:36621
(匿名函数)application.js:36921
DeferredActionQueues.flush application.js: 34001
Backburner.end application.js:34092
Backburner.run application.js:34131
Ember.run application.js:34489
hash.success application.js:74468
fire application.js: 3049
self.fireWith application.js:3161
done application.js:8236
callback
谢谢
我有同样的问题。在你的rails serializer中,(假设你有一个),你应该执行
attributes:id,:name,:start_date
并有一个
在您的日程表串行器中,$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $让我知道这是否有效。I am using ember-data and the activemodel adapter with rails and mongoid(mongodb) in the backend. Whenever I make a request to my rails app, emberjs displays the returned data but in chrome developer console it displays:
Assertion failed: You must include an `id` in a hash passed to `push`
the problem reproduced in a jsfiddle
I have reproduced the problem in this jsfiddle. You can see a different version of thesame jsfiddle working when I use id instead of _id.
Payload sent by the backend
ActiveModel adapter converts the snake_case start_date to camel-case. I have added some custom code to ember-data rest-serializer to convert _id to id and that code is pasted lower down in this question.
{"schedules":[ { "_id":"529f38596170700787000000", "name":"Hair styling", "start_date":"2013-12-12" } ]}
Now though the returned payload includes an id, if I go into the google cgrome console and run the commmands below and access the _data, it shows the id is undefined.
a = App.__container__.lookup("controller:schedules") b = a.get('content')
I f use the disclosure arrows in the console and dig into the _dat object, this is what I see
_data: Object id: undefined
Custom serializer code
I extended the activemodeladapter to convert mongodb's _id to id and to set it as primary key.
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({ primaryKey: '_id', normalize: function(type, hash, prop) { this.normalizeMongoidId(hash); return this._super(type, hash, prop); }, normalizeMongoidId: function(hash){ var json = { id: hash._id }; delete hash._id; //hash.id = hash._id; //delete hash._id; } /* serialize: function(record, options){ var json = this._super(record, options); json.schedule._id = json.record.id; delete json.record.id return json; } */ });
The ember-data model:
App.Schedule = DS.Model.extend({ name: DS.attr('string'), organization: DS.belongsTo('organization') });
Here is my emberjs router
App.Router.map(function(){ this.resource('schedules', {path: "/schedules"}, function(){ this.resource('schedule', {path: "/:schedule_id"}, function(){}); }); });
The emberjs route defination:
App.SchedulesRoute = Ember.Route.extend({ model: function(){ return this.store.find('schedule'); } });
emberjs stack trace
Assertion failed: You must include an `id` in a hash passed to `push` application.js:31601 (anonymous function) application.js:31601 Ember.assert application.js:28431 DS.Store.Ember.Object.extend.push application.js:69415 (anonymous function) application.js:69483 Ember.EnumerableUtils.map application.js:30115 DS.Store.Ember.Object.extend.pushMany application.js:69482 (anonymous function) application.js:69839 invokeCallback application.js:36783 (anonymous function) application.js:36838 EventTarget.trigger application.js:36621 (anonymous function) application.js:36921 DeferredActionQueues.flush application.js:34001 Backburner.end application.js:34092 Backburner.run application.js:34131 Ember.run application.js:34489 hash.success application.js:74468 fire application.js:3049 self.fireWith application.js:3161 done application.js:8236 callback
Thanks
解决方案I had the same issue. In your rails serializer for schedule, (assuming you have one), you should do
attributes :id, :name, :start_date
and have a
has_many :schedule
in your SchedulesSerializer. Let me know if this works.
这篇关于ember-data-1.0.0 activemodeladapter错误包括传递给“push”的散列中的“id”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!