Optional add () for many-to-many connection
Can I add multiple of the same objects to a many-to-many connection? The current setup I'm running gives me Error: Trying to '.add()' an instance which already exists!
every time I try to add a speaker multiple times. For example, perhaps speaker X speaks for 20 minutes, speaker Y takes over, and then its speaker X: s turns again. How can I solve this?
this is my event model:
attributes: {
id: {
type: "integer",
primaryKey: true,
autoIncrement: true
},
name: {
type: "string",
required: true
},
speakers: {
collection: "speaker",
via: 'events',
dominant: true
}
},
addSpeaker: function (options, cb) {
Event.findOne(options.id).exec(function (err, event) {
if (err) return cb(err);
if (!event) return cb(new Error('Event not found.'));
event.speakers.add(options.speaker);
event.save(cb);
});
And also the speaker model:
attributes: {
name: {
type: "string",
required: true
},
title : {
type: "string"
},
event: {
model: "event"
},
events: {
collection: "event",
via: "speakers"
}
}
+3
source to share
No one has answered this question yet
Check out similar questions: