ember.js Ember 3.25属于非侧载

2o7dmzc5  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(254)

我试图从一个API调用中侧载关系数据。我有一个我的两个模型设置如下:
竞赛模型(子项,属于):

import Model, { attr, belongsTo } from '@ember-data/model';

export default class ContestModel extends Model {
  @attr('date') createdAt;
  @attr('string') description;

  @belongsTo('brand', { inverse: 'contests' }) brand;
}

品牌模型(父代,hasMany):

import Model, { attr, hasMany } from '@ember-data/model';

export default class BrandModel extends Model {
  @attr('date') createdAt;
  @attr('string') name;

  @hasMany('contest') contests;
}

我通过这条线获取:

this.store.findRecord('contest', params.contest_id, { include: 'brand' });

这是返回的API有效负载:

{
  "data": {
    "type": "contest",
    "id": 1,
    "attributes": {
      "body_json": [
        {
          "question": "what time is it?",
          "answers": ["1PM", "2PM", "3PM", "4PM"]
        },
        {
          "question": "which collection is this artwork from?",
          "answers": ["botanical garden collection", "ghibli collection", "adventure collection", "swag collection"]
        }
      ],
      "created_at": "2021-05-23 18:00:00 -0700",
      "description": "a good description goes here"
    },
    "relationships": {
      "brand": {
        "links": {
          "self": "http://example.com/contests/2/relationships/brand" // not sure what this does
        },
        "data": { "type": "brand", "id": 2 }
      }
    },
    "links": {
      "self": "http://example.com/contests/2" // not sure how this is useful
    },
    "included": [
      {
        "id": 2,
        "type": "brand",
        "attributes": {
          "created_at": "2021-05-23 20:00:00 -0700",
          "name": "aloha vibe"
        }
      }
    ]
  }
}

这里的问题是调用myContest.brand返回一个Proxy对象,就像在这个SO post中一样。
是我的API负载不正确,还是我的模型配置错误?还是其他什么?我在Ember 3.25.3
从注解更新:当我将async: false添加到Ember Data查询中时,由于toReturn.isEmpty为true,我在www.example.com这一行后面收到一个错误https://github.com/emberjs/data/blob/v3.25.0/packages/store/addon/-private/system/model/internal-model.ts#L705。我是否没有配置我的API负载?不确定是什么错误。看起来createdAtname都没有填充。

vwkv1x7d

vwkv1x7d1#

发现错误!数据负载需要采用以下格式:

{
  "data": {
    "type": "contest",
    "id": 1,
    "attributes": {
      "body_json": [
        {
          "question": "what time is it?",
          "answers": ["1PM", "2PM", "3PM", "4PM"]
        },
        {
          "question": "which collection is this artwork from?",
          "answers": ["botanical garden collection", "ghibli collection", "adventure collection", "swag collection"]
        }
      ],
      "created_at": "2021-05-23 18:00:00 -0700",
      "description": "a good description goes here"
    },
    "relationships": {
      "brand": {
        "links": {
          "self": "http://example.com/brands/2"
        },
        "data": { "type": "brand", "id": 2 }
      }
    },
    "links": {
      "self": "http://example.com/contests/2"
    }
  },
  "included": [
    {
      "id": 2,
      "type": "brand",
      "attributes": {
        "created_at": "2021-05-23 20:00:00 -0700",
        "name": "aloha vibe"
      }
    }
  ]
}

我已经将included嵌套在data部分中,但它应该与data部分一起位于顶层。现在,事情已经正确地侧加载,并且没有进行额外的API调用:)

相关问题