app/models/card.js
export default Model.extend({
title: attr('string'),
description: attr('string'),
users: hasMany('user')
});
app/models/user.js
export default Model.extend({
email: attr('string'),
name: attr('string')
});
app/routes/card/new.js
actions: {
save(title, description) {
const newCard = this.get('store').createRecord('card', {
title,
description
});
this.get('store').findRecord('user', 1).then(function(user) {
newCard.set('users', user);
console.log(newCard);
});
newCard.save().then((card) => {
// go to the new item's route after creating it.
this.transitionTo('card.card', card);
});
}
}
所以,当我保存一张 card
时,它抛出了这个错误:
断言失败:您必须传递一组记录才能设置 hasMany 关系
。
我想在新创建的 card
和 user
之间建立关联。
其他信息:
repo 链接:https://github.com/ghoshnirmalya/hub-client
Ember :2.6.1
Ember 数据:2.6.1
最佳答案
你定义一张卡有很多用户:
users: hasMany('user')
因此您可以将“用户”设置为单个用户:
newCard.set('users', user);
因此,您可以将 hasMany
更改为 belongsTo
,或者将 'users' 设置为一个 user
数组用户:[用户]
关于javascript - Ember js 中的 hasMany 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38289037/