Ember JS 3.1为什么id没有加载hasMany关系



这是我的版本。

$ ember -v 
ember-cli: 3.1.4
node: 8.10.0
os: linux x64

我正在尝试使用一个有很多关系的ID进行搜索。有一个承诺,但我不知道在哪里等它。

const user = this.get('user');
return user.get('accounts').then((accounts) => {
console.log(accounts); // Class {canonicalState: Array(0), store: Class, relationship: ManyRelationship, type: ƒ, record: InternalModel, …}
console.log(accounts.isLoaded); // true
console.log(accounts.length); // 0
const ids = accounts.mapBy('id');
console.log(ids); // <-- Why is this empty?
return this.store.query('order', { filter: { accounts: ids } });
}

这也没有如预期的那样起作用。

const user = this.get('user');
const accountsRef = user.hasMany('accounts'); // HasManyReference {store: Class, internalModel: InternalModel, hasManyRelationship: ManyRelationship, type: "account", parent: RecordReference}
const ids = accountsRef.ids(); // []

这也以同样的方式失败了。

const user = this.get('user');
const promise = new RSVP.Promise((resolve, reject) => {
return user.get('accounts').then((accounts) => {
resolve(accounts);
});
return promise.then((data) => {
console.log(data); // // Class {canonicalState: Array(0), store: Class, relationship: ManyRelationship, type: ƒ, record: InternalModel, …}
const ids = accounts.mapBy('id');
console.log(ids); // []
});

User上的hasmany关系定义为accounts: DS.hasMany('account'),反序列化程序使用records

这在Handlebar模板中工作,并生成我感兴趣的ID列表

{{#each user.accounts as |account|}}
{{account.id}} - {{account.name}}
{{/each}}

模型挂钩和模板渲染之间发生了什么?我还需要在哪里查找故障排除?

问题不是Ember或Ember数据。我的API没有响应嵌入记录或关联的id。请检查你的API是否与规范匹配。

最新更新