简体   繁体   English

Ember js 中的 hasMany 关联

[英]hasMany association in Ember js

app/models/card.js应用程序/模型/card.js

export default Model.extend({
  title: attr('string'),
  description: attr('string'),
  users: hasMany('user')
});

app/models/user.js应用程序/模型/user.js

export default Model.extend({
  email: attr('string'),
  name: attr('string')
});

app/routes/card/new.js应用程序/路由/卡/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);
    });
  }
}

So, when I'm saving a card , it is throwing me this error:因此,当我保存card ,它向我抛出了这个错误:

Assertion Failed: You must pass an array of records to set a hasMany relationship . Assertion Failed: You must pass an array of records to set a hasMany relationship

I want to create an association between the newly created card and the user .我想在新创建的carduser之间创建关联。

Other info:其他信息:

Repo link: https://github.com/ghoshnirmalya/hub-client回购链接: https : //github.com/ghoshnirmalya/hub-client

Ember : 2.6.1余烬:2.6.1

Ember Data : 2.6.1灰烬数据:2.6.1

You defined that a card has many users:你定义了一张卡片有很多用户:

users: hasMany('user')

So you can's set 'users' to a single user:因此,您可以将“用户”设置为单个用户:

newCard.set('users', user);

So you could either change the hasMany to a belongsTo , or set the 'users' instead to just one user to an array of users: [user]因此,您可以将hasMany更改为belongsTo ,或者将“用户”设置为仅一个user到一组用户: [user]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM