Strapi Hooks Not Creating Relations Correctly

System Information
  • Strapi Version: 4.6.1

Hi!

I am having some trouble.

I have two collections. Case and Consultant. I have a relation between the two, so that if I add a case to a consultant, the consultant is also added to the case. Now, I have defined a component called CaseConsultantRelation. This component has a case and a caseRole. I’ve added this component to my Consultant collection.

What I want to do, is when I add a case relation to a consultant, i want it to automaically be put in the CaseConsultantRelation, so that I can write the Role manually.

I have the following lifecycle:

module.exports = {
async beforeCreate(event) {
if (event.params.data.cases && event.params.data.cases.connect && event.params.data.cases.connect.length > 0) {
console.log(‘beforeCreate’);

  let caseToConnect = event.params.data.cases.connect[0].id;
  console.log("caseToConnect: " + caseToConnect)

  await strapi.query('generel.role-element').create({
    data: {
      case: caseToConnect,
      roleDescription: "role description here",
    },
  }); 
}

},

async beforeUpdate(event) {
if (event.params.data.cases && event.params.data.cases.connect && event.params.data.cases.connect.length > 0) {
console.log(‘beforeUpdate’);

  let caseToConnect = event.params.data.cases.connect[0].id;
  console.log("caseToConnect: " + caseToConnect)

  await strapi.query('generel.role-element').create({
    data: {
      case: caseToConnect,
      roleDescription: "role description here",
    },
  }); 
}

},
};

However, when I add a relation between cases and consultant, it does not create an entry for CaseConsultantRelation (the display name for role-element). How can i fix this?

As I am quite new to Strapi, any help would be greatly appreciated!