Uncaught Error: Assertion Failed: You may not set `id` as an attribute on your model

删除回忆录丶 提交于 2019-12-23 08:55:31

问题


Entire error:

Uncaught Error: Assertion Failed: You may not set `id` as an attribute on your model. Please remove any lines that look like: `id: DS.attr('<type>')` from App.Plan

Ember Model:

App.Plan = DS.Model.extend({
id: DS.attr('number'),
name: DS.attr('string'),
period: DS.attr('number'),
price: DS.attr('number')
});

Data coming from REST API:

{"plans":[{"id":1,"name":"Monthly subscription","period":1,"price":2}]}

Question: How can I move around the above error ?


回答1:


Just leave out the id attribute from your DS.Model. Ember-data automatically handles that for you. You'll still be able to find the record using the id, and it'll be a property on the object when you access it from within your Ember application. See here and here for more info.

App.Plan = DS.Model.extend({
  name: DS.attr('string'),
  period: DS.attr('number'),
  price: DS.attr('number')
});


来源:https://stackoverflow.com/questions/24559017/uncaught-error-assertion-failed-you-may-not-set-id-as-an-attribute-on-your-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!