apache / apache/pouchdb

PouchDB find error: selector containing `[null]` throws by non-matching document

Open
#8,900 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
17.6k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

Error when selecting array containing `null` against non-existing value:
```javascript
it("should not throw", async () => {
const db = context.db;

await db.put({ _id: '1' });

// FIXME: exception is thrown if document field not exists
// and selector contains a null in an array
const resp = await db.find({
selector: { field: [null] }
})

resp.docs.should.have.length(0);
});
```

```console
TypeError: Cannot read properties of undefined (reading '0')
at getFieldFromDoc (packages/node_modules/pouchdb-selector-core/lib/index.js:14:18)
at packages/node_modules/pouchdb-selector-core/lib/index.js:434:32
at Array.every ()
at matchSelector (packages/node_modules/pouchdb-selector-core/lib/index.js:417:33)
at packages/node_modules/pouchdb-selector-core/lib/index.js:405:12
at Array.every ()
at rowFilter (packages/node_modules/pouchdb-selector-core/lib/index.js:397:25)
at packages/node_modules/pouchdb-selector-core/lib/index.js:374:12
at Array.filter ()
at Object.filterInMemoryFields (packages/node_modules/pouchdb-selector-core/lib/index.js:373:15)
at packages/node_modules/pouchdb-find/lib/index.js:1454:40
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
```

With CouchDB 3.1.2 (same used in GH CI workflows):
```bash
# database connection
db=http://admin:password@localhost:5984/test

# write document
curl -s -H "Content-Type: application/json" -X PUT $db/1 -d '{}' | jq .

# query database
curl -s -H "Content-Type: application/json" -X POST $db/_find -d '{"selector":{ "field":[null] }}' | jq .
```
Output:
```console
{
"ok": true,
"id": "1",
"rev": "1-967a00dff5e02add41819138abb3284d"
}
{
"docs": [],
"bookmark": "nil",
"warning": "No matching index found, create an index to optimize query time."
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.