我目前正在使用 Rails + Ember 3.12 构建软件,但遇到了一个奇怪的问题。
我的模型如下:
// test-case-run
import DS from 'ember-data';
const { Model } = DS;
export default Model.extend({
testCase: DS.belongsTo('test-case'),
testCaseRunLogs: DS.hasMany('test-case-run-logs')
});
// test-case-run-log
import DS from 'ember-data';
const { Model } = DS;
export default Model.extend({
testCaseRun: DS.belongsTo('test-case-run'),
payload: DS.attr('')
});
而且,我的后端正在返回以下负载:
{
"data": {
"id": "83",
"type": "test_case_run",
"relationships": {
"test_case": {
"data": {
"id": "90",
"type": "test_case"
}
},
"test_case_run_logs": {
"data": []
}
}
}
}
{
"data": {
"id": "83",
"type": "test_case_run",
"relationships": {
"test_case": {
"data": {
"id": "90",
"type": "test_case"
}
},
"test_case_run_logs": {
"data": [
{
"id": "426",
"type": "test_case_run_log"
}
]
}
}
},
"included": [
{
"id": "426",
"type": "test_case_run_log",
"attributes": {
"payload": "SCREENSHOT"
},
"relationships": {
"test_case_run": {
"data": {
"id": "83",
"type": "test_case_run"
}
}
}
}
]
}
我有一个自定义适配器定义:
pathForType(type) {
return underscore(pluralize(type));
}
所以,我认为一切都应该顺利。
看来我的关系加载不正常。 而且,我无法访问任何数据,例如:
log.get('testCaseRun') // that is null
run.get('testCaseRunLogs.length') // it returns 0
这很奇怪,因为我的记录已加载到商店中,但没有加载它们的关系。 我不知道如何解决这个问题,因为我可以从 ember 获得的信息量非常有限(没有错误,格式看起来不错,......)。
有人可以帮助我了解我的电话有什么问题吗?我已经尝试了很多事情,例如重命名我的模型,但这并没有改善这种情况。
此外,这个模型是唯一一个我有问题的模型。我所有的其他型号都没有这个问题。所以,这有点奇怪。
非常感谢
最佳答案
unknown
在 <(unknown):ember264>
指类的名称。这并不意味着您的关系没有正确加载。它只是使用匿名类的 Ember 数据。
要查看关系数据,您可以单击该字符串,然后单击 content
。 .另一种选择是使用 $E
将完整记录传递到控制台。链接在右上角。之后您可以与控制台上的记录进行交互,例如做一个$E.get('testCaseRun.id')
.
顺便说一句:如果模型名称与破折号属性名称匹配,则无需在关系定义中显式声明模型名称。所以testCaseRun: DS.belongsTo('test-case-run')
与testCaseRun: DS.belongsTo()
相同.
关于javascript - Ember 未知关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998996/