我有一个rails后端,根据jsonapi标准(jsonapi-resources )提供数据服务。我有两个模型: Contact和PhoneNumber。
class Contact < ApplicationRecord
has_many :phone_numbers
end
class PhoneNumber < ApplicationRecord
belongs_to :contact
end这是API端点的JSON响应。
{
"data": [
{
"id": "6",
"type": "phone-numbers",
"links": {
"self": "http://localhost:3000/phone-numbers/6"
},
"attributes": {
"name": "byeworld",
"phone-number": "9212098"
},
"relationships": {
"contact": {
"links": {
"self": "http://localhost:3000/phone-numbers/6/relationships/contact",
"related": "http://localhost:3000/phone-numbers/6/contact"
}
}
}
}
]
}这些是我的Ember模型:
联系:
import DS from 'ember-data';
export default DS.Model.extend({
nameFirst: DS.attr('string'),
nameLast: DS.attr('string'),
email: DS.attr('string'),
twitter: DS.attr('string'),
phoneNumbers: DS.hasMany('phone-number', {async: true, inverse: 'contact'})
});电话号码:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
phoneNumber: DS.attr('string'),
contact: DS.belongsTo('contact', {async: true, inverse: 'phoneNumbers'})
});这是我的路由处理程序:
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('contact', params.contact_id, {include: "phone-numbers"});
},
(...)
});通过以下操作,我无法访问模板上联系人的电话号码:
{{#each model.phoneNumbers as |phone|}}
{{phone.name}}
{{/each}}此外,电话号码的数据存在于余烬控制台中。我遗漏了什么?
发布于 2017-07-20 15:39:32
有一个悬而未决的问题,在成员-数据版本2.14.3。因此,请将您的余烬数据版本降至2.13.2。也许能解决你的问题。
参考本余烬-数据公开问题- https://github.com/emberjs/data/issues/4942
发布于 2017-07-20 15:23:24
model.PhoneNumbers => model.phoneNumbers
如果这不仅仅是这个错误,尝试从JSON适配器/序列化器扩展您的模型序列化程序/适配器(如果您的所有模型都使用它,则在您的应用程序序列化程序/适配器上这样做):
import DS from 'ember-data'; export default DS.JSONAPIAdapter.extend({});
以及:
import JSONAPISerializer from 'ember-data/serializers/json-api'; export default JSONAPISerializer.extend({});
https://stackoverflow.com/questions/45198702
复制相似问题