cube-js/cube · error · Error

Last argument should be a function

Error message

Last argument should be a function

What it means

dynRef treats its last argument as the function that computes the reference SQL and everything before it as reference names. If the final argument is not a function it throws, since the reference cannot be evaluated.

Source

Thrown at packages/cubejs-schema-compiler/src/extensions/Reflection.ts:15

import R from 'ramda';
import { DynamicReference } from '../compiler/DynamicReference';
import { AbstractExtension } from './extension.abstract';

export class Reflection extends AbstractExtension {
  public dynRef = (...args) => {
    if (args.length < 2) {
      throw new Error('List of references and a function are expected in form: dynRef(\'ref\', (r) => (...))');
    }

    const references = R.dropLast(1, args);
    const fn = args[args.length - 1];

    if (typeof fn !== 'function') {
      throw new Error('Last argument should be a function');
    }

    return new DynamicReference(references, fn);
  };
}

View on GitHub (pinned to 7d981676b3)

Solutions

  1. Ensure the last argument is a function: dynRef('ref', (r) => `...` )
  2. If references and the function are in an array, keep the function as the final element
  3. Log/console-check the arguments being spread into dynRef when constructing them dynamically

Example fix

// before
dynRef('users', 'count(*)');
// after
dynRef('users', (r) => `count(*)`);
Defensive patterns

Strategy: validation

Validate before calling

const lastIsFn = (...args) => typeof args[args.length - 1] === 'function';

Type guard

const lastIsFn = (...args) => typeof args[args.length - 1] === 'function';

Try / catch

try {
  const ref = reflection.dynRef(...dynRefArgs);
} catch (e) {
  if (e.message === 'Last argument should be a function') {
    console.error('dynRef requires its final argument to be a callback function');
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling dynRef('ref', 'notAFunction'), dynRef('ref', someObject), or building args where the callback is missing or in the wrong position.

Common situations: Passing a string of SQL instead of a function; forgetting the arrow function; accidentally reordering arguments so a string lands last; a variable expected to be a function being undefined due to an import error.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


AI-assisted analysis of cube-js/cube@7d981676b3 (2026-09-02). Data as JSON: /api/errors/389a41078c3c1c24. Report an issue: GitHub.