There is a one to many relationship between schemaA and schemaB. If I call schemaA.remove(); I want all related schemaB to be removed. If I call schemaB.remove() I want the reference to schemaB in instanceA to be removed.
Using
SchemaA
.pre('remove', function (next) {
var instanceBIdArray = this.instanceBIdArray;
async.map(instanceBIdArray, function (instanceBId, callback) {
SchemaB.findByIdAndRemove(instanceBId, function (err, instanceB) {
if (err) callback(err);
if (!instanceB) callback('instanceB not found.');
callback(null, instanceBId);
});
},
function (err, results) {
if (err) next(new Error('Something went wrong with deleting instanceB.'));
return next();
});
});
And
SchemaB
.pre('remove', function (next) {
var self = this;
SchemaA.findByIdAndUpdate(self.instanceAId, {
$pull: {
instanceBIdArray: self._id
}
}, function (err, instanceA) {
if (err) next(new Error('Something went wrong with deleting instanceA.'));
return next();
});
});
in each of the mongoose schemas respectively seems to be the right way to approach this. However, I can only get one of the two to work. If I have both express will not compile, and it will say that schemaB does not exist.
I believe this has something to do with require.js and that in order to export schemaA requirejs needs to export schemaB and vice versa. Any insight to this is greatly appreciated!
The error is: MissingSchemaError: Schema hasn't been registered for model "A".