I’m trying to add data to a model called variant but it doesn’t work. I don’t know what the problem is anymore because it doesn’t show me the console.log that I put in either in the browser console or in the terminal.
This is my complete lifecycles.js code:
const { createCombinations } = require('../../../../utils/combinations.js');
module.exports = {
async afterCreate(event) {
console.log('Hook afterCreate ejecutado');
await generateVariants(event);
},
async afterUpdate(event) {
console.log('Hook afterUpdate ejecutado');
await generateVariants(event);
},
async afterPublish(event) {
console.log('Hook afterPublish ejecutado');
await generateVariants(event);
}
};
async function generateVariants(event) {
const { result } = event;
console.log('Resultado del evento:', result);
const variantOptions = result.variant_options;
console.log('Opciones de variantes:', variantOptions);
if (variantOptions && variantOptions.length > 0) {
const valuesArray = variantOptions.map((option) => option.values);
const combinations = createCombinations(valuesArray);
console.log('Combinaciones generadas:', combinations);
for (const combination of combinations) {
try {
if (!result.documentId) {
console.error('El producto no tiene un documentId válido.');
continue;
}
const newVariant = await strapi.db.query('api::variant.variant').create({
data: {
variant_name: combination.join(' / '),
product: result.id,
barcode: "no_tiene_codigobarras",
publishedAt: new Date(),
},
});
console.log('Variante creada con éxito:', newVariant);
} catch (error) {
console.error('Error creando variante:', error);
}
}
} else {
console.log('No se encontraron opciones de variantes para generar combinaciones.');
}
}
This topic has been created from a Discord post (1302842626080505906) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord