检索数据后,Ember未设置hasMany关系属性

时间:2019-05-31 16:45:34

标签: ember.js ember-data

Foreward:该项目有很多创举,例如构建我的第一个API,第一次使用JSON API,第一次使用Ember,第一次在SO上发布等等。

我正在尝试访问包含最新更新的模板中的'作者'hasMany属性。我在模板中访问它的内容,但是什么也没有返回。模型已正确保存,但未设置关系,因为在履行承诺的情况下,DS.PromiseManyArraylatest.contentAuthors的长度为0{ _length: 0, isFulfilled: true, isRejected: false } )。

我正在使用Ember 3.10(带有CLI)。我可以完全控制自己的后端(运行ExpressionEngine 5的LAMP),并且可以通过自定义的内置插件来处理API请求,尽管我不确定这太重要了,因为从我所知这似乎主要是前端问题。

路线

import Route from '@ember/routing/route';

export default Route.extend({
    model(){
        let latest = this.store.peekAll('latest');
        if (latest.length < 2){
            latest = this.store.query('latest', { limit: 2, include: "people" });
        }
        return latest;
    }
});

基本模型

import DS from 'ember-data';
const { Model } = DS;

export default Model.extend({
    title: DS.attr()
});

最新型号
编辑:删除了原始代码中没有的冗余属性

import DS from 'ember-data';
import ExpressionEngineBase from './expression-engine-base';

export default ExpressionEngineBase.extend({
    blurb: DS.attr(),
    contentAuthors: DS.hasMany('person')
});

人员模型
编辑:删除了原始代码中没有的冗余属性

import DS from 'ember-data';
import ExpressionEngineBase from './expression-engine-base';

export default ExpressionEngineBase.extend({
    latest: DS.hasMany('latest')
});

模板

{{#each this.model as |latest|}}
    <h2>{{latest.title}}</h2>
    {{#each latest.contentAuthors as |author|}}
        <div>{{author.title}}</div>
    {{else}}
        <div>Can't find author(s)</div>
    {{/each}}
    <p>{{latest.blurb}}</p>
{{/each}}

从服务器发送的数据
编辑:针对JSON API validator进行了检查,以发现原始文档不符合要求。我已经更新了后端,但这不能解决问题。现在符合该验证程序的要求。

{
    "data": [{
        "id": "3161",
        "type": "latest",
        "attributes": {
            "title": "Amazing Video 1"
        },
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/video\/3161"
        },
        "relationships": {
            "people": {
                "data": [{
                    "id": "1",
                    "type": "people"
                }]
            }
        }
    }, {
        "id": "2573",
        "type": "latest",
        "attributes": {
            "title": "Amazing Article 1"
        },
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/white_papers_insights\/2573"
        },
        "relationships": {
            "people": {
                "data": [{
                    "id": "1",
                    "type": "people"
                }, {
                    "id": "52",
                    "type": "people"
                }]
            }
        }
    }],
    "links": {
        "self": "https:\/\/cms.example.com\/api\/v1\/latest?include=people&limit=2"
    },
    "included": [{
        "id": "1",
        "type": "people",
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/people\/1",
            "channel": "https:\/\/cms.example.com\/api\/v1\/people"
        },
        "attributes": {
            "title": "Great Author"
        }
    }, {
        "id": "52",
        "type": "people",
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/people\/52",
            "channel": "https:\/\/cms.example.com\/api\/v1\/people"
        },
        "attributes": {
            "title": "Great Co-Author"
        }
    }]
}

重申一下,该关系模型正在保存并且可以在Ember Inspector中查看,但是未设置实际的链接/关系。

更新: :我尝试重命名类型和查询参数均无济于事,请检查是否出现了词尾变化与删除的关系有关。

1 个答案:

答案 0 :(得分:0)

模型中的关系名为contentAuthors,但在有效负载中,其命名为people

所以代替这个:

"people": {
  "data": [{
    "id": "1",
    "type": "people"
  }]
}

执行此操作:

"contentAuthors": {
  "data": [{
    "id": "1",
    "type": "people"
  }]
}

如果您的模型中有contentAuthors: DS.hasMany('person')


边注:latest不是很好的模型名称。型号名称应始终为名词,或至少以一个结尾。