Skip to content

(#9183) - Fix null pointer exception when searching with index for missing field#9199

Open
brucearctor wants to merge 2 commits intoapache:masterfrom
brucearctor:9183-fix-null-pointer-missing-indexed-field
Open

(#9183) - Fix null pointer exception when searching with index for missing field#9199
brucearctor wants to merge 2 commits intoapache:masterfrom
brucearctor:9183-fix-null-pointer-missing-indexed-field

Conversation

@brucearctor
Copy link

@brucearctor brucearctor commented Mar 15, 2026

Overview

When performing a find() query that uses an index, if one of the documents in the database is missing the indexed field, filterInclusiveStart throws a TypeError: can't convert null to object from inside pouchdb-collate.

filterInclusiveStart calls getKeyFromDoc to extract the indexed key from each document. When a document does not have the indexed field, getKeyFromDoc returns undefined for that field. This undefined value is then passed to collate(), which crashes.

This fix adds a guard in filterInclusiveStart to skip any document whose key is undefined (i.e. the document is missing the indexed field). This matches standard CouchDB behaviour, where documents missing a field are excluded from an index.

Testing recommendations

A regression test has been added in tests/find/test-issues/test.issue9183.js:

  • Inserts two documents: one with the indexed field (name), one without
  • Creates an index on name
  • Queries with $gt: null and verifies only the document with the field is returned
  • Previously this would throw a TypeError

Run with: TYPE=find PLUGINS=pouchdb-find GREP=issue9183 npm test

Related Issues or Pull Requests

Fixes #9183

Checklist

  • I am not a bot
  • This is my own work, I did not use AI, LLM's or similar technology for code or docs generation
  • Code is written and works correctly
  • Changes are covered by tests
  • Documentation changes were made in the docs folder

…for missing field

filterInclusiveStart throws a TypeError when collating a key
for a document that is missing the indexed field. This commit adds
a check to skip documents where the required index key is undefined,
matching CouchDB's behavior of excluding such documents from the index.
@janl
Copy link
Member

janl commented Mar 18, 2026

could you please ensure you use the PR template?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pouchdb-find exception when searching with index for not existing field

2 participants