{"record":{"id":"4df9082876024102","repo":"appsmithorg/appsmith","slug":"updatemodel-expects-an-object-as-parameter","errorCode":null,"errorMessage":"updateModel expects an object as parameter","messagePattern":"updateModel expects an object as parameter","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/client/src/widgets/CustomWidget/component/customWidgetscript.js","lineNumber":259,"sourceCode":"          if (typeof fn !== \"function\") {\n            throw new Error(\"onModelChange expects a function as parameter\");\n          }\n\n          modelSubscribers.push(fn);\n          fn(window.appsmith.model);\n\n          return () => {\n            // Unsubscribe from model changes\n            const index = modelSubscribers.indexOf(fn);\n\n            if (index > -1) {\n              modelSubscribers.splice(index, 1);\n            }\n          };\n        },\n        updateModel: (obj) => {\n          if (!obj || typeof obj !== \"object\") {\n            throw new Error(\"updateModel expects an object as parameter\");\n          }\n\n          appsmith.model = Object.assign(\n            Object.assign({}, appsmith.model),\n            obj,\n          );\n\n          // Send an update model message to the parent\n          channel.postMessage(EVENTS.CUSTOM_WIDGET_UPDATE_MODEL, obj);\n        },\n        triggerEvent: (eventName, contextObj) => {\n          if (typeof eventName !== \"string\") {\n            throw new Error(\"eventName should be a string\");\n          } else if (contextObj && typeof contextObj !== \"object\") {\n            throw new Error(\"contextObj should be an object\");\n          }\n\n          // Send a trigger event message to the parent","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/appsmithorg/appsmith/blob/8cd9021c24cdbea1c3c12c966073708e83db60c2/app/client/src/widgets/CustomWidget/component/customWidgetscript.js#L241-L277","documentation":"Thrown by window.appsmith.updateModel() in a legacy Custom Widget iframe. updateModel merges a partial object into appsmith.model via Object.assign and forwards the delta to the parent with a CUSTOM_WIDGET_UPDATE_MODEL message. The guard rejects null/undefined and any non-object primitive because Object.assign on a primitive would silently no-op or corrupt the model.","triggerScenarios":"Calling appsmith.updateModel(null), appsmith.updateModel(undefined), appsmith.updateModel('foo'), appsmith.updateModel(42), or appsmith.updateModel() with no argument. Note typeof [] === 'object', so arrays are NOT rejected by this guard but will misbehave when merged.","commonSituations":"Conditionally building a patch object that ends up undefined when no field changed; passing a serialized string instead of an object; spreading a possibly-undefined variable (appsmith.updateModel(maybePatch)) without a default.","solutions":["Always pass a plain object literal: appsmith.updateModel({ key: value }).","Guard optional patches: if (patch) appsmith.updateModel(patch); or default to {} : appsmith.updateModel(patch ?? {}).","Construct the patch in a typed local object before sending so it can never be a primitive.","Avoid passing arrays; wrap list data under a key, e.g. updateModel({ items: [...] })."],"exampleFix":"// before\nappsmith.updateModel(selectedItem); // selectedItem may be null\n\n// after\nappsmith.updateModel({ selected: selectedItem });","handlingStrategy":"validation","validationCode":"// Build the patch as a guaranteed object\nconst patch = maybePatch ?? {};\nif (typeof patch !== 'object' || patch === null) {\n  throw new TypeError('updateModel needs a plain object');\n}\nappsmith.updateModel(patch);","typeGuard":"const isPlainObject = (v) =>\n  v !== null && typeof v === 'object' && !Array.isArray(v);\n\n// usage\nif (isPlainObject(patch)) appsmith.updateModel(patch);","tryCatchPattern":"try {\n  appsmith.updateModel(patch ?? {});\n} catch (e) {\n  console.error('updateModel rejected patch:', e.message);\n}","preventionTips":["Never pass a primitive; always wrap a value under an object key.","Default optional patches with ?? {} before sending.","Reject arrays at the call site to avoid silently storing [object] merges.","Type the patch in TypeScript as Record<string, unknown> to catch misuse at compile time."],"tags":["custom-widget","appsmith-api","parameter-validation","javascript"],"backgroundTag":null,"analyzedSha":"8cd9021c24cdbea1c3c12c966073708e83db60c2","analyzedAt":"2026-08-12T22:14:19.293Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}