I'm trying to destroy multiple relations linked to a model from my angular application.
in my person.json I have two relations:
"messageGroups": {
"type": "hasMany",
"model": "messageGroup"
},
"messageStudents": {
"type": "hasMany",
"model": "messageStudent"
}
These work fine. However I want to delete multiple messageGroups and messageStudents for a person in one call. Rather than use multiple calls to Person.messageGroups.destroyById(...) and Person.messageStudents.destroyById(...)
My initial thought was to make a remote method in my person.js file that accepts a personId and an object containing ids for messageGroups and messageStudents.
But I'm unable to write working code. My last attempt looks like this:
Person.destroyTargetsFromMessage = function (personId, assignees, callback) {
for(var i in assignees.studentIds){
Person.messageStudents.destroyById({'id': personId, 'fk': assignees.studentIds[i]});
}
for(var i in assignees.groupIds){
Person.messageGroups.destroyById({'id': personId, 'fk': assignees.groupIds[i]});
}
callback();
};
Or would the proper way to do this use multiple calls to my API after all?
UPDATE
I've written this code that brings me closer to my goal:
Person.destroyTargetsFromMessage = function (personId, assignees, callback) {
Person.findById(teacherId, function(err, targetPerson){
for(var i in assignees.studentIds){
targetPerson.messageStudents.findById(assignees.studentIds[i], function(err, student){
callback(null, student);
console.log(student);
});
}
for(var i in assignees.groupIds){
targetPerson.messageGroups.findById(assignees.groupIds[i], function(err, group){
callback(null, group);
console.log(group);
});
}
callback();
});
};
The only problem is that I cannot call destroyById on my targetPerson relations, but findById seems to be working just fine.
SOLVED
the method that deletes my relations is destroy
, not destroyById
the method that deletes my relations is destroy
, not destroyById