Ember.js ember-data-1.0.0 activemodeladapter错误在传递给“push”的哈希中包含“id”`
我正在使用ember数据和activemodel适配器,并在后端使用rails和mongoid(mongodb)。每当我向rails应用程序发出请求时,emberjs都会显示返回的数据,但在chrome开发者控制台中,它会显示:Ember.js ember-data-1.0.0 activemodeladapter错误在传递给“push”的哈希中包含“id”`,ember.js,ember-data,ember-router,Ember.js,Ember Data,Ember Router,我正在使用ember数据和activemodel适配器,并在后端使用rails和mongoid(mongodb)。每当我向rails应用程序发出请求时,emberjs都会显示返回的数据,但在chrome开发者控制台中,它会显示: Assertion failed: You must include an `id` in a hash passed to `push` 问题在JSFIDLE中重现 Assertion failed: You must include an `id` in
Assertion failed: You must include an `id` in a hash passed to `push`
问题在JSFIDLE中重现
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
我在这篇文章中重现了这个问题。当我使用id而不是\u id时,您可以看到相同的不同版本正在工作
后端发送的有效负载
ActiveModel适配器将snake\u case开始日期转换为camel case。我已经在ember data rest序列化程序中添加了一些自定义代码,以将\u id转换为id,并且在这个问题中,该代码粘贴到了较低的位置
{"schedules":[
{
"_id":"529f38596170700787000000",
"name":"Hair styling",
"start_date":"2013-12-12"
}
]}
现在,尽管返回的有效负载包含id,但如果我进入google cgrome控制台并运行下面的命令并访问\u数据,则表明id未定义
a = App.__container__.lookup("controller:schedules")
b = a.get('content')
如果使用控制台中的显示箭头并深入查看\u dat对象,这就是我看到的
_data: Object
id: undefined
自定义序列化程序代码
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
我扩展了activemodeladapter,将mongodb的_id转换为id,并将其设置为主键
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;
}
*/
});
余烬数据模型:
App.Schedule = DS.Model.extend({
name: DS.attr('string'),
organization: DS.belongsTo('organization')
});
App.SchedulesRoute = Ember.Route.extend({
model: function(){
return this.store.find('schedule');
}
});
这是我的emberjs路由器
App.Router.map(function(){
this.resource('schedules', {path: "/schedules"}, function(){
this.resource('schedule', {path: "/:schedule_id"}, function(){});
});
});
emberjs路线定义:
App.Schedule = DS.Model.extend({
name: DS.attr('string'),
organization: DS.belongsTo('organization')
});
App.SchedulesRoute = Ember.Route.extend({
model: function(){
return this.store.find('schedule');
}
});
emberjs堆栈跟踪
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
谢谢,我也有同样的问题。在您的rails序列化程序for schedule中(假设您有一个),您应该这样做
attributes :id, :name, :start_date
祝你生日快乐
has_many :schedule
在您的日程表序列化程序中。让我知道这是否有效。你能给出服务器给浏览器的错误和响应的完整堆栈跟踪吗余烬错误很奇怪,但从你的日志来看,错误似乎在Rails服务器上(响应错误500),请给我们您的
调度程序\u controller.rb
?@adrencioquio完整的调度程序\u controller.rb粘贴在标题为从emberjs到的部分下面rails@Hardik127,则来自emberjs侧的堆栈跟踪粘贴在问题下方。对于rails方面,我之前粘贴的是返回的所有堆栈跟踪。我检查了chrome控制台中返回的对象,它没有id,甚至它收到的json也有id。这是有效负载{“schedules”:[{“id”:{“$oid”:“529F38596170700077000000”},“name”:“Hair styleing”,“description”:null,“start_date”:“2013-12-12”,“end_date”:null,“organization”:null}我在JSFIDLE中重现了未解决的问题,因为我已经解决了所有与后端相关的问题。但是,仍然会抛出emberjs错误。这是你的电话号码。当我使用id而不是_id时,您可以看到相同的工作方式。