Ember 3.25 BelongsTo 不侧载
Ember 3.25 BelongsTo not sideloading
我正在尝试从一个 API 调用中加载关系数据。我的两个模型设置如下:
比赛模型(child,属于):
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;
}
品牌型号(parent,有很多):
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
returns 代理 object 就像在 .
中一样
我的 API 负载是否不正确,或者我的模型配置有误?或者是其他东西?我在 Ember 3.25.3
评论更新:当我将 async: false
添加到 Ember 数据查询时,由于 toReturn.isEmpty
为真,我在 https://github.com/emberjs/data/blob/v3.25.0/packages/store/addon/-private/system/model/internal-model.ts#L705 行之后收到错误。我没有配置我的 API 负载吗?不知道哪里出了问题。 createdAt
和 name
似乎都没有填充。
发现错误!数据负载需要采用这种格式:
{
"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 调用 :)
我正在尝试从一个 API 调用中加载关系数据。我的两个模型设置如下:
比赛模型(child,属于):
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;
}
品牌型号(parent,有很多):
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
returns 代理 object 就像在
我的 API 负载是否不正确,或者我的模型配置有误?或者是其他东西?我在 Ember 3.25.3
评论更新:当我将 async: false
添加到 Ember 数据查询时,由于 toReturn.isEmpty
为真,我在 https://github.com/emberjs/data/blob/v3.25.0/packages/store/addon/-private/system/model/internal-model.ts#L705 行之后收到错误。我没有配置我的 API 负载吗?不知道哪里出了问题。 createdAt
和 name
似乎都没有填充。
发现错误!数据负载需要采用这种格式:
{
"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 调用 :)