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.