Loopbackjs 如何删除通过关系创建的相关项?

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(); }); }; 其中

我有3个模型:模型A、模型B和模型C

模型“A”通过模型“C”与模型“B”具有关系(HasManyThrough)

如何删除相关项目

module.exports = function(ModelA) {
    ModelA.beforeRemote('deleteById', function(context, remoteMethodOutput, next) {

        //Remove relationships

        next();
    });
};

其中一些考虑:

  • 调试上下文以实际查找id随请求发送的位置。我使用泛型示例获取modelA的id

  • 要删除相关的modelB实例,请单击modelAInstance.modelB.destroyAll(),其中modelB是关系的名称,“modelA有许多modelB”


  • 是否要先删除相关项目?这是你的问题吗?
     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();
     });