meilisearch / meilisearch-vue

https://www.meilisearch.com/
MIT License
154 stars 13 forks source link

Error: The "searchParameters" option expects an object. #137

Closed mimartinez closed 4 months ago

mimartinez commented 4 months ago

Description I have a problem with component AisConfigure. I get an error when I use it, the error is The "searchParameters" option expects an object..

The same error it looks like in the example for Vue3 that you have in the CodeSandbox https://codesandbox.io/p/sandbox/ms-vue3-is-forked-wsrkl8

Basic example <ais-configure :hits-per-page.camel="12" :page="0" />

Screenshots or Logs Captura de pantalla 2024-04-30 a las 0 21 48

Environment:

mimartinez commented 4 months ago

Solved by creating a custom component AisCustomConfigure

import { createWidgetMixin, createSuitMixin } from 'vue-instantsearch/vue3/es';
import { isVue3, renderCompat } from 'vue-instantsearch/vue3/es/src/util/vue-compat/index-vue3';

export default {
  name: 'AisCustomConfigure',
  mixins: [
    createSuitMixin({ name: 'CustomConfigure' }),
    createWidgetMixin(
      { connector: connectConfigure },
      {
        $$widgetType: 'ais.customConfigure'
      }
    )
  ],
  computed: {
    widgetParams() {
      return {
        // To fix the error: The "searchParameters" option expects an object
        searchParameters: JSON.parse(JSON.stringify(this.$attrs))
      };
    }
  },
  render: renderCompat(h => {
    const slot = isVue3 ? this?.$slots?.default : this?.$scopedSlots?.default;

    if (!this?.state || !slot) {
      return null;
    }

    return h(
      'div',
      {
        class: this.suit()
      },
      [
        slot({
          refine: this.state.refine,
          searchParameters: this.state.widgetParams.searchParameters
        })
      ]
    );
  })
};

calling it so in the ais-instant-search

<ais-instant-search
        :search-client="searchClient"
        index-name="video-library:updated_at_timestamp:desc"
      >
        <ais-custom-configure :hits-per-page.camel="12" :page="0"/>
</ais-instant-search>

The problem is in this.$attrs which is not as a JSON