System Information
- Strapi Version: 3.6.2
- Operating System: Mac/Windows
- Database: sqLite
- Node Version: v10.16.0
- NPM Version: 6.9.0
- Yarn Version: -
Hi, I am creating some sort of an application skills display table where every user can set his skill-level. Use Case:
I create a new application with a post request. When the application is created, every user is added to the new application. Everytime an application is added, every existing user should get a new component with the new application and a default skill level. I got as far as creating the application and adding each user. Where I struggle is with updating each user with the component. I figured out how to “inject” it into each user with a forEach loop. But I fail with the update function of the user database. I actually called the user employees.
I tried it with a lifecyclehook afterCreate on the employees Model:
'use strict';
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
* to customize this model
*/
module.exports = {
/**
* Triggered before user creation.
*/
lifecycles : {
async afterCreate(result, data) {
console.log('lifecycle data ', data);
console.log('lifecycle result ', result);
const allEmployees = await strapi.query('employees').find();
// update new app with all existing employees
allEmployees.forEach(employee => {
employee.applications_skilllevels.push({
application : result.id,
skill_level : 5
});
});
try {
allEmployees.forEach(async employee => {
await strapi.services.employees.update({ id: employee.id }, allEmployees);
});
} catch (error) {
console.log('error', error);
}
return sanitizeEntity(allEmployees, { model: strapi.models.employees });
}
}
};
I hope my explanation is clear.
Any Ideas where I go wrong?