If statement for default search parameter

I have a scenario where I need default filters to be applied on page load, but not all the time.

I have a variable for example called var2 and then

const search = instantsearch({
appId: ‘myappid’,
apiKey: ‘myapiKey’,
indexName: ‘mydb’,
urlSync: true,
searchParameters: {
filters: ‘category:’+var2
}
});

What I’d like to achieve is:

if(var2) {

const search = instantsearch({
appId: ‘myappid’,
apiKey: ‘myapiKey’,
indexName: ‘mydb’,
urlSync: true,
searchParameters: {
filters: ‘category:’+var2
}
});

} else {

const search = instantsearch({
appId: ‘myappid’,
apiKey: ‘myapiKey’,
indexName: ‘mydb’,
urlSync: true
}
});

}

but this doesn’t work and I know I must be doing something wrong ? Can anyone point me in the right direction please.

Thanks

Guy

Hi Guy,

What I would do in your case is use the Configure widget, which is a headless widget that let you configure the settings of your search, for example the filter used. If your condition is met, then add this configure widget with the filter. Let me know if it works for you.

Thanks,
Guy