algolia / instantsearch

⚡️ Libraries for building performant and instant search and recommend experiences with Algolia. Compatible with JavaScript, TypeScript, React and Vue.
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/
MIT License
3.73k stars 525 forks source link

Cannot read properties of null (reading 'hits') #6404

Open bitttttten opened 1 month ago

bitttttten commented 1 month ago

🐛 Current behavior

We see that on Sentry we get a lot of the error "Cannot read properties of null (reading 'hits')".

Screenshot 2024-10-19 at 12 17 53

It's hard to investigate for us as the stack trace leads us from ../../../../node_modules/instantsearch.js/es/connectors/autocomplete/connectAutocomplete.js in indices at line 63:84 to ../../../../node_modules/instantsearch.js/es/lib/utils/defer.js in progress at line 18:16.

Our dependencies are:

  "dependencies": {
    "algoliasearch": "^4.22.1",
    "react-instantsearch": "^7.5.5",
    "react-instantsearch-nextjs": "0.2.4",
    "search-insights": "^2.13.0"
  },

This project is not using nextjs, but we are in a monorepo and so are sharing some packages. We are just importing from react-instantsearch for things like useInfiniteHits, useHits, useRefinementList, the Configure component.

We have about 26 files using react-instantsearch so I'm happy to share some code examples although it's hard for us to know which part is causing the issue. So I wanted to open this issue incase it is a library bug. Happy to try and work through to understand if it's userland or not.

🔍 Steps to reproduce

  1. Use react-instantsearch
  2. See this error reported in Sentry

Live reproduction

n/a

💭 Expected behavior

No error is thrown

Package version

react-instantsearch@7.5.5

Operating system

No response

Browser

No response

Code of Conduct

Haroenv commented 1 month ago

The most interesting part to reproduce would be the part where you're using connectAutocomplete. I wonder if it's possibly rendering conditionally and therefore at some timing where there's no results? It's odd because this is in a loop with the scopedResults array which therefore is definitely present, but it seems to imply that one of the results is null. Could this maybe be an adjacent index widget which is rendered conditionally as well?

We could guard for the case but I'd rather find out why results is null before, maybe something larger is wrong?

bitttttten commented 1 month ago

What could help you here? Shall I share some more of our set up code?

Haroenv commented 1 month ago

a minimal reproduction that shows the issue is the most efficient way for us to dig down.