{"record":{"id":"89f0b7f3d55f516f","repo":"dianping/cat","slug":"cannot-call-val-if-initselection-is-not-define","errorCode":null,"errorMessage":"cannot call val() if initSelection() is not defined","messagePattern":"cannot call val\\(\\) if initSelection\\(\\) is not defined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cat-home/src/main/webapp/assets/js/uncompressed/select2.js","lineNumber":2493,"sourceCode":"                this.select\n                    .val(val)\n                    .find(\"option\").filter(function() { return this.selected }).each2(function (i, elm) {\n                        data = self.optionToData(elm);\n                        return false;\n                    });\n                this.updateSelection(data);\n                this.setPlaceholder();\n                if (triggerChange) {\n                    this.triggerChange({added: data, removed:oldData});\n                }\n            } else {\n                // val is an id. !val is true for [undefined,null,'',0] - 0 is legal\n                if (!val && val !== 0) {\n                    this.clear(triggerChange);\n                    return;\n                }\n                if (this.opts.initSelection === undefined) {\n                    throw new Error(\"cannot call val() if initSelection() is not defined\");\n                }\n                this.opts.element.val(val);\n                this.opts.initSelection(this.opts.element, function(data){\n                    self.opts.element.val(!data ? \"\" : self.id(data));\n                    self.updateSelection(data);\n                    self.setPlaceholder();\n                    if (triggerChange) {\n                        self.triggerChange({added: data, removed:oldData});\n                    }\n                });\n            }\n        },\n\n        // single\n        clearSearch: function () {\n            this.search.val(\"\");\n            this.focusser.val(\"\");\n        },","sourceCodeStart":2475,"sourceCodeEnd":2511,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-home/src/main/webapp/assets/js/uncompressed/select2.js#L2475-L2511","documentation":"Error \"cannot call val() if initSelection() is not defined\" thrown in dianping/cat.","triggerScenarios":"Triggered when val() is called on a Select2 attached to an <input type='hidden'> without defining initSelection() to map ids to objects.","commonSituations":"See trigger scenarios.","solutions":["Define the initSelection option when attaching Select2 to a hidden input so val() can resolve ids to data.","Set data via select2('data', ...) instead of relying on val()."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}