strapi-plugin-meilisearch
strapi-plugin-meilisearch copied to clipboard
Meilisearch fails to add entry when "Draft and publish" is enabled in collection
Description
Meilisearch plugin for Strapi fails to add entry when "Draft & publish" is enabled in collection. It shows the following error and you will always have to manually synchronize in Meilisearch menu:
error: Meilisearch could not add entry with id: 6: Transaction query already complete, run with DEBUG=knex:tx for more info
Screenshots or Logs
Environment:
- OS: MacOS 15.3.2
- Meilisearch version: v.1.9.1
- strapi-plugin-meilisearch version: v0.13.2
- Strapi version: v5.12.4
just updated my project to strapi 5 and i'm seeing the same behaviour
just updated my project to strapi 5 and i'm seeing the same behaviour
Hi @francescob,
I’ve come up with a temporary solution based on @ratorik’s comments in issue #997. I’m sharing the code snippet below in case it helps you.
I used the global lifecycle of Strapi (src/index.ts), specifically within the bootstrap function, to register a subscriber on the afterCreate event.
This ensures that synchronization with Meilisearch only happens when the publishedAt property is not null.
This approach is still provisional, but it has been working well for my use case.
Here’s a sample code snippet:
`
import type { Core } from '@strapi/strapi';
export default {
register(/* { strapi }: { strapi: Core.Strapi } */) {},
bootstrap({ strapi }: { strapi: Core.Strapi } ) {
// registering a subscriber
strapi.db.lifecycles.subscribe({
async afterCreate(event) {
const { data, where, select, populate } = event.params;
const { result, model } = event;
if (result.publishedAt) {
const store = strapi.plugin('meilisearch').service('store');
const indexed = await store.getIndexedContentTypes();
if (indexed.includes(model.uid)) {
const meilisearch = strapi.plugin('meilisearch').service('meilisearch');
await meilisearch
.addEntriesToMeilisearch({
contentType: model.uid,
entries: [result],
})
.catch(e => {
strapi.log.error(
`***********Meilisearch could not add entry with id: ${result.id}: ${e.message}`,
)
});
}
}
},
});
},
};
`
thanks @ejstavares , that helped a lot!