strapi-plugin-meilisearch icon indicating copy to clipboard operation
strapi-plugin-meilisearch copied to clipboard

Meilisearch fails to add entry when "Draft and publish" is enabled in collection

Open ejstavares opened this issue 8 months ago • 3 comments

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

Image

Environment:

  • OS: MacOS 15.3.2
  • Meilisearch version: v.1.9.1
  • strapi-plugin-meilisearch version: v0.13.2
  • Strapi version: v5.12.4

ejstavares avatar Apr 12 '25 01:04 ejstavares

just updated my project to strapi 5 and i'm seeing the same behaviour

francescob avatar Apr 15 '25 13:04 francescob

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}`,
                    )
                });
        }
      }        
    },
    });
  },
};

`

ejstavares avatar Apr 16 '25 12:04 ejstavares

thanks @ejstavares , that helped a lot!

francescob avatar Apr 16 '25 13:04 francescob