未捕获错误:断言失败:您不能将`id`设置为模型的属性 [英] Uncaught Error: Assertion Failed: You may not set `id` as an attribute on your model
本文介绍了未捕获错误:断言失败:您不能将`id`设置为模型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
整个错误:
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模型:
App.Plan = DS.Model.extend({
id: DS.attr('number'),
name: DS.attr('string'),
period: DS.attr('number'),
price: DS.attr('number')
});
来自REST API的数据:
Data coming from REST API:
{"plans":[{"id":1,"name":"Monthly subscription","period":1,"price":2}]}
问题:如何解决上述错误?
推荐答案
只需从 DS.Model $中省略
id
C $ C>。 Ember数据自动为您处理。您仍然可以使用id查找记录,并且当您从Ember应用程序中访问该对象时,它将是对象上的一个属性。 请参阅这里和 here 获取更多信息。
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')
});
这篇关于未捕获错误:断言失败:您不能将`id`设置为模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文