elemMatch throws TypeError: value is null if array has null element

Open Beginner friendly
#9,103 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
javascript
Domain
databases

Research direction

Start in index.es.js at the $elemMatch matcher and its rowFilter call, following the stack trace around getFieldFromDoc. Reproduce the issue with an array containing an object followed by null, then verify that matching no longer throws and preserves the intended elemMatch result.

Written by the indexing model from the issue text.

Description

pouchdb-find throws TypeError: value is null if array has null element on non-first position.
Example:
{ test: [{ foo: "blub"}, null] }

Exception:

TypeError: value is null
    getFieldFromDoc index.es.js:10
    rowFilter index.es.js:396
    rowFilter index.es.js:393
    elemMatch index.es.js:557
    $elemMatch index.es.js:556
    match index.es.js:473
    matchSelector index.es.js:417
    matchSelector index.es.js:413
    rowFilter index.es.js:401
    rowFilter index.es.js:393

Code:

var matchers = {
  "$elemMatch": function(doc, userValue, parsedField, docFieldValue) {
    if (!Array.isArray(docFieldValue)) {
      return false;
    }
    if (docFieldValue.length === 0) {
      return false;
    }
    if (typeof docFieldValue[0] === "object" && docFieldValue[0] !== null) {
      return docFieldValue.some(function(val) {
        return rowFilter(val, userValue, Object.keys(userValue)); // << val is null
      });
    }
[...]
Dominant language
JavaScript
Stars
17.6k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/pouchdb

All issues in apache/pouchdb

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.