Loopbackjs 如何删除通过关系创建的相关项?
我有3个模型:模型A、模型B和模型C 模型“A”通过模型“C”与模型“B”具有关系(HasManyThrough) 如何删除相关项目Loopbackjs 如何删除通过关系创建的相关项?,loopbackjs,Loopbackjs,我有3个模型:模型A、模型B和模型C 模型“A”通过模型“C”与模型“B”具有关系(HasManyThrough) 如何删除相关项目 module.exports = function(ModelA) { ModelA.beforeRemote('deleteById', function(context, remoteMethodOutput, next) { //Remove relationships next(); }); }; 其中
module.exports = function(ModelA) {
ModelA.beforeRemote('deleteById', function(context, remoteMethodOutput, next) {
//Remove relationships
next();
});
};
其中一些考虑:
是否要先删除相关项目?这是你的问题吗?
ModelA.beforeRemote('deleteById', function(context, remoteMethodOutput, next) {
//get the id of ModelA from context object.
var id = context.req.id;
//find the model instance to delete.
ModelA.findById(id, function(err, modelAInstance) {
if (err) throw err;
//destroy all ModelB instance related to modelAInstance.
modelAInstance.modelB.destroyAll({}, function(err, info) {
if (err) throw err;
console.log(info);
});
});
next();
});