appsmithorg/appsmith · error · Error

contextObj should be an object

Error message

contextObj should be an object

What it means

Thrown by the else-if branch of window.appsmith.triggerEvent() in a legacy Custom Widget iframe. After eventName passes its string check, the optional contextObj is validated: if it is truthy but not an object, the call is rejected because contextObj is serialized into the CUSTOM_WIDGET_TRIGGER_EVENT payload sent to the parent.

Source

Thrown at app/client/src/widgets/CustomWidget/component/customWidgetscript.js:274

        },
        updateModel: (obj) => {
          if (!obj || typeof obj !== "object") {
            throw new Error("updateModel expects an object as parameter");
          }

          appsmith.model = Object.assign(
            Object.assign({}, appsmith.model),
            obj,
          );

          // Send an update model message to the parent
          channel.postMessage(EVENTS.CUSTOM_WIDGET_UPDATE_MODEL, obj);
        },
        triggerEvent: (eventName, contextObj) => {
          if (typeof eventName !== "string") {
            throw new Error("eventName should be a string");
          } else if (contextObj && typeof contextObj !== "object") {
            throw new Error("contextObj should be an object");
          }

          // Send a trigger event message to the parent
          channel.postMessage(EVENTS.CUSTOM_WIDGET_TRIGGER_EVENT, {
            eventName,
            contextObj,
          });
        },
        model: {},
        ui: {},
        onReady: (fn) => {
          if (typeof fn !== "function") {
            throw new Error("onReady expects a function as parameter");
          }

          onReady = fn;

          if (isReady && !isReadyCalled && onReady) {

View on GitHub (pinned to 8cd9021c24)

Solutions

  1. Wrap context data in an object: appsmith.triggerEvent('click', { value: someValue }).
  2. Pass null or undefined explicitly when there is no context: appsmith.triggerEvent('ready').
  3. If forwarding a DOM event, extract a plain serializable subset: appsmith.triggerEvent('change', { value: e.target.value }).

Example fix

// before
appsmith.triggerEvent('search', searchText); // string, not object

// after
appsmith.triggerEvent('search', { query: searchText });
Defensive patterns

Strategy: validation

Validate before calling

// contextObj is optional; only validate when provided
const ctx = contextObj == null ? undefined : contextObj;
if (ctx !== undefined && (typeof ctx !== 'object' || ctx === null)) {
  throw new TypeError('triggerEvent contextObj must be an object');
}
appsmith.triggerEvent(eventName, ctx);

Type guard

const isContextObject = (v) =>
  v == null || (typeof v === 'object' && !Array.isArray(v));

// usage
if (isContextObject(detail)) appsmith.triggerEvent(name, detail);

Try / catch

try {
  appsmith.triggerEvent(name, detail ? { value: detail } : undefined);
} catch (e) {
  console.error('triggerEvent rejected context:', e.message);
}

Prevention

When it happens

Trigger: Calling appsmith.triggerEvent('click', 'clicked'), appsmith.triggerEvent('click', 42), or appsmith.triggerEvent('click', Symbol()). null/undefined are allowed (the truthy check skips them); only truthy primitives are rejected.

Common situations: Passing a single value instead of wrapping it in an object; forwarding a DOM event's target string instead of an event-detail object; passing a form value directly as context.

Related errors


AI-assisted analysis of appsmithorg/appsmith@8cd9021c24 (2026-08-12). Data as JSON: /api/errors/7bc75ac5858a76c5. Report an issue: GitHub.