#9103·pouchdb

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

Author: smesgr9000Created Jul 24, 2025Updated Jun 14, 2026

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
      });
    }
[...]