Adding custom search filter throws Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'totalResults')

I have a requirement where i need to show the facets with all the facet values without even making the search. i'm able to capture the userinput and store it in state. when the user is trying to search i'm adding the user selection in the beforeSearchCall in the AppSearchAPIConnector as below. but i am getting the below error. any help is appreciated.
Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'totalResults')

     beforeSearchCall: (options, next) => {
       console.log('queryoptions', options);
       if(!countrySelected&&!languageSelected)
       next({
         ...options,
         filters: [
          {
            field: 'regioncountrylist',
            values: [countrySelected],
            type: 'any',
          },
          {
            field: 'language',
            values: [languageSelected],
            type: 'any'
          }
        ]
       })

Hi @Mecadamia.nut :

I'm not very familiar with the Search UI library, but I believe you are not returning any value from the beforeSearchCall function when the if clause is not satisfied.

You should invoke next without the extra filters in an else branch to ensure you cover that case.

Can you please try it, or let me know what the next invocation returns so I can keep digging?

Thanks Carlos. I've tried this and it is still throwing the same error. Below is the error followed by the modified code. Appreciate you checking on this

Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'totalResults')

(anonymous function)

C:/Apps/OCD/techdocs/src/SearchDriver.js:349

  346 | };  347 |   348 | return this.events.search(requestState, queryConfig).then(> 349 |   resultState => {      | ^  350 |     if (this.searchRequestSequencer.isOldRequest(requestId)) return;  351 |     this.searchRequestSequencer.completed(requestId);  352 | 

beforeSearchCall: (existingSearchOptions, next) => {
if(!countrySelected&&!languageSelected)
next({
...existingSearchOptions,
filters: [
{ field: 'regioncountrylist',values: [countrySelected],type: 'any'},
{field: 'language',values: [languageSelected],type: 'any'}
]
})
else
next({...existingSearchOptions})
}

I think the problem is that you're not explicitly returning your values. You should explicitly add the return statement:

     beforeSearchCall: (options, next) => {
       console.log('queryoptions', options);
       if(!countrySelected&&!languageSelected)
       return next({
         ...options,
         filters: [
          {
            field: 'regioncountrylist',
            values: [countrySelected],
            type: 'any',
          },
          {
            field: 'language',
            values: [languageSelected],
            type: 'any'
          }
        ]
       })

Have a look at other options for achieving this in this post.

Thanks Carlos. i've modified my code and unfortunately...it is not even added to the search call. I've used the onSearch in the post you have mentioned and is working fine now. Thanks for your help.

            onSearch: (state, queryConfig, next) => {
              return next(
                {
                  ...state,
                  filters: [
                    { field: "language",values: [languageSelected] }
                  ]
                },
                queryConfig
              );
            },  

when i add multiple filters, i'm getting below error.
An unexpected error occurred: [400] Filters contains an invalid value for item inside of field: filters root; must be an object

         onSearch: (state, queryConfig, next) => {
            return next(
              {
                ...state,
                filters: [
                  { field: "regioncountrylist",values: [countrySelected],type: 'all' },
                  { field: "language",values: [languageSelected],type: 'all' }
                ]
              },
              queryConfig
            );

when i change the code to below. i'm getting below error. Any idea of what i'm doing wrong?

An unexpected error occurred: filters.map is not a function

         onSearch: (state, queryConfig, next) => {
            return next(
              {
                ...state,
                filters: {
                  all: [
                    { "regioncountrylist": countrySelected },
                    { "language": languageSelected }
                  ]
               },
              queryConfig
            );

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.