Skip to content

Cache key expired or removed throws with "Cannot read properties of undefined (reading 'v')" #329

Description

@twrayden

When using node-cache in a TypeScript/Node.js project, I encounter the following error when calling .get() or .del() on a key that is missing or expired:

{
      "type": "TypeError",
      "message": "Cannot read properties of undefined (reading 'v')",
      "stack":
          TypeError: Cannot read properties of undefined (reading 'v')
              at NodeCache._unwrap (/app/node_modules/node-cache/lib/node_cache.js:711:19)
              at NodeCache.get (/app/node_modules/node-cache/lib/node_cache.js:368:23)
              at setDocumentCache (file:///app/dist/utils/document.js:18:36)
              at newDocumentFromData (file:///app/dist/utils/document.js:34:9)
              at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
              at async Object.getPdfMetadata (file:///app/dist/plugins/live-document-service.js:36:33)
              at async Object.<anonymous> (file:///app/dist/routes/preview/pdf-metadata.js:86:19)
    }

Steps to Reproduce:

  1. Create a cache instance with deleteOnExpire: false
  2. Call .get() on a key that does not exist or has expired.
  3. The error occurs inside the library when it tries to access .v of undefined.
const documentCache = new NodeCache({
  stdTTL: 60 * 2, // 2 minutes
  useClones: false,
  deleteOnExpire: false,
});

documentCache.on("expired", (key, pdfDocument) => {
  if (typeof pdfDocument?.cleanup === "function") {
    pdfDocument.cleanup();
  }
  documentCache.del(key);
});

function setDocumentCache(cacheKey: string, pdfDocument: PDFDocumentProxy) {
  const existing = documentCache.get<PDFDocumentProxy>(cacheKey);
  if (typeof existing?.cleanup === "function") {
    existing.cleanup();
  }
  documentCache.set(cacheKey, pdfDocument);
}

Expected Behavior:

Calling .get() on a missing or expired key should return undefined without throwing an error.

Environment:

  • node-cache version: 5.1.2
  • Node.js version: lts-bullseye

Additional Context:

Seems to be triggered by the library’s internal _unwrap method.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions