Problem with Entity Service API: CRUD operations

System Information
  • Strapi Version: 4.4.1.
  • Operating System: elementary OS 6.1 Jólnir Linux
  • Database: Sqlite (local)
  • Node Version: 16.13.1
  • NPM Version: 8.19.2
  • Yarn Version:

Hi.
I have /src/api/sastanak/content-types/sastanak/lifecycles.js with this code:

module.exports = {
	async afterUpdate(event) {
		const { result } = event;
		if (result.Prisutni) {
			await strapi.entityService.update('api::sastanak.sastanak', result.id, {
				data: {
					Odrzan: true,
				},
			});
		}
	}
}

Basically, if there is anything in the text field “Prisutni”, I need my boolean switch “Odrzan” to be true.

When I click the “Save” button to finish editing my entry, all I got is a completely frozen Strapi, without any errors in the browser console or terminal, while the “Save” button is spinning without end. When I do ctrl+c in the terminal, I cannot restart Strapi with npm run develop because I get Sqlite database locked errors.

What is making me completely crazy, is that I have the exact same code on another model and it works flawlessly.

Any help would be greatly appreciated.

Cheers.

Solved by using beforeUpdate lifecycle hook:

module.exports = {
	async beforeUpdate(event) {
		const { data } = event.params;
		if (data.Prisutni) {
			data.Odrzan = true;
		} else {
			data.Odrzan = false;
		}
	}
}
1 Like