Danilo Delizia
06/16/2024, 8:04 AMimport { config } from "@/config";
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";
const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
server: {
apiKey: config.typesense.apiKey, // Be sure to use an API key that only allows search operations
nodes: config.typesense.nodes,
cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
},
// The following parameters are directly passed to Typesense's search API endpoint.
// So you can pass any parameters supported by the search endpoint below.
// query_by is required.
additionalSearchParameters: {
query_by: config.typesense.queryBy,
},
});
export const searchClient = typesenseInstantsearchAdapter.searchClient;
export const getValueOfFacet = async (facetName: string) => {
const indexName = config.typesense.collectionName
const values = await searchClient.searchForFacetValues([{
indexName,
params: {
facetName,
facetQuery: '',
},
}]);
return values
}
I have now a react server component:
export async function FacetValues() {
const facets = await getValueOfFacet("myfacet")
return (<div>{
JSON.stringify(facets)
}
</div>)
}
When I import this component I get the following error:
✓ Compiled in 648ms (347 modules)
TypeError: Cannot read properties of undefined (reading 'counts')
at FacetSearchResponseAdapter._adaptFacetHits (webpack-internal:///(rsc)/../../node_modules/.pnpm/typesense-instantsearch-adapter@2.8.0_@babel+runtime@7.24.6/node_modules/typesense-instantsearch-adapter/lib/FacetSearchResponseAdapter.js:32:29)
at FacetSearchResponseAdapter.adapt (webpack-internal:///(rsc)/../../node_modules/.pnpm/typesense-instantsearch-adapter@2.8.0_@babel+runtime@7.24.6/node_modules/typesense-instantsearch-adapter/lib/FacetSearchResponseAdapter.js:45:25)
at eval (webpack-internal:///(rsc)/../../node_modules/.pnpm/typesense-instantsearch-adapter@2.8.0_@babel+runtime@7.24.6/node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:112:40)
at Array.map (<anonymous>)
at TypesenseInstantsearchAdapter._callee2$ (webpack-internal:///(rsc)/../../node_modules/.pnpm/typesense-instantsearch-adapter@2.8.0_@babel+runtime@7.24.6/node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:108:60)
at tryCatch (webpack-internal:///(rsc)/../../node_modules/.pnpm/@babel+runtime@7.24.6/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:45:16)
at Generator.eval (webpack-internal:///(rsc)/../../node_modules/.pnpm/@babel+runtime@7.24.6/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:133:17)
at Generator.eval [as next] (webpack-internal:///(rsc)/../../node_modules/.pnpm/@babel+runtime@7.24.6/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:74:21)
at asyncGeneratorStep (webpack-internal:///(rsc)/../../node_modules/.pnpm/@babel+runtime@7.24.6/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17)
at _next (webpack-internal:///(rsc)/../../node_modules/.pnpm/@babel+runtime@7.24.6/node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
⨯ TypeError: Cannot read properties of undefined (reading 'counts')
at Array.map (<anonymous>)
digest: "314892750"
⨯ TypeError: Cannot read properties of undefined (reading 'counts')
at Array.map (<anonymous>)
digest: "314892750"
GET / 500 in 2509ms
Not sure what I am doing wrong here... any hint?Jason Bosco
06/17/2024, 2:13 AMsearchForFacetValues
is meant to be a method to be used by Instantsearch directly, and so the method parameters have a specific format that is managed by instantsearchJason Bosco
06/17/2024, 2:16 AMtypesenseInstantsearchAdapter.searchClient.searchForFacetValues
, I would recommend using typesenseInstantsearchAdapter.typesenseClient
which is just an instance of typesense-js, and call the Typesense API directly