将许多文档串起来

我正在尝试使用节点和续集来创build项目和任务之间的多对多关联。 我不知道如何创build新的“任务”与variables名称,可以作为一个数组积累为project.setTasks函数,因为这里的文档给出了作为一个例子ALOT的细节丢失。 http://sequelizejs.com/documentation#associations-many-to-many

这里是他们给的例子:

Project.hasMany(Task) Task.hasMany(Project) Project.create()... Task.create()... Task.create()... // save them... and then: project.setTasks([task1, task2]).success(function() { // saved! 

})

我不确定“variables名称”是什么意思。

不过,下面是续集testing套件的一个例子,如下所示:

  // Define the models this.User = this.sequelize.define('User', { username: DataTypes.STRING }); this.Task = this.sequelize.define('Task', { title: DataTypes.STRING, active: DataTypes.BOOLEAN }); // Setup associations this.User.hasMany(this.Task); this.Task.hasMany(this.User); // using promises, sync the database, then create a user and two tasks return this.sequelize.sync({ force: true }).then(function() { return Promise.all([ self.User.create({ username: 'John'}), self.Task.create({ title: 'Get rich', active: true}), self.Task.create({ title: 'Die trying', active: false}) ]); }).spread(function (john, task1, task2) { // This line below is only relevant to the tests. self.tasks = [task1, task2]; // Using the created (and saved) tasks and user, set the tasks and return the promise return john.setTasks([task1, task2]); }); 

编辑我同意续集文档仍然太轻,但我发现它是一个很好的框架到目前为止使用。 我build议您在怀疑的时候阅读unit testing,或者编写自己的testing来确认行为。

编辑注意你也可以使用.addTask.removeTask而不是简单地设置它们全部。 看文档