Fetching the data from external api and I following the florianmrz . I try someting simillar to i can create and update the products by i can’t sincronize the localizations. Someone knows why?
f
const locals = await getLocales();
let localizations = [];
let fail = false;
let defaultLocale = await strapi.db.query(`api::product.product`).findOne({where: {apiId: id, locale: DEFAULT_LOCALE}});
for(let l=0; l<locals.length; l++){
let local = locals[l];
localizations[l] = getParsedProductByLang(bod.data, id, local);
if(local == DEFAULT_LOCALE){
if(!!defaultLocale){
await strapi.entityService.update('api::product.product', defaultLocale.id, { data: localizations[l], populate: '*'});
}else{
defaultLocale = await strapi.entityService.create('api::product.product', { data: localizations[l], populate: '*' });
fail = true;
}
localizations[l].id = defaultLocale.id;
}else{
let currentLocale = await strapi.db.query(`api::product.product`).findOne({where: {apiId: id, locale: local}});
if(!!currentLocale){
localizations[l].id = currentLocale.id;
currentLocale = await strapi.entityService.update('api::product.product', currentLocale.id, { data: localizations[l], populate: '*' });
}else{
currentLocale = await strapi.entityService.create('api::product.product', { data: localizations[l], populate: '*' });
fail = true;
}
localizations[l].id = currentLocale.id;
}
}
//console.log(localizations);
let mainLocale = await strapi.entityService.update('api::product.product', defaultLocale.id, {
data: {
localizations: localizations.map((entry) => entry.id),
},
populate: ['localizations'], // localizations needs to be populated for the upcoming `syncLocalizations()` call
});
await strapi.plugin('i18n')
.service('localizations')
.syncLocalizations(mainLocale, { model: strapi.getModel('api::product.product') });