{"record":{"id":"072b4b1947d86420","repo":"bilibili/flv.js","slug":"cannot-convert-undefined-or-null-to-object","errorCode":null,"errorMessage":"Cannot convert undefined or null to object","messagePattern":"Cannot convert undefined or null to object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/utils/polyfill.js","lineNumber":31,"sourceCode":" * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nclass Polyfill {\n\n    static install() {\n        // ES6 Object.setPrototypeOf\n        Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {\n            obj.__proto__ = proto;\n            return obj;\n        };\n\n        // ES6 Object.assign\n        Object.assign = Object.assign || function (target) {\n            if (target === undefined || target === null) {\n                throw new TypeError('Cannot convert undefined or null to object');\n            }\n\n            let output = Object(target);\n            for (let i = 1; i < arguments.length; i++) {\n                let source = arguments[i];\n                if (source !== undefined && source !== null) {\n                    for (let key in source) {\n                        if (source.hasOwnProperty(key)) {\n                            output[key] = source[key];\n                        }\n                    }\n                }\n            }\n            return output;\n        };\n\n        // ES6 Promise (missing support in IE11)\n        if (typeof self.Promise !== 'function') {","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/bilibili/flv.js/blob/42343088f22619cf014a057b3878fe3d320016a6/src/utils/polyfill.js#L13-L49","documentation":"This is a polyfilled ES6 Object.assign in src/utils/polyfill.js that mirrors the native spec: per spec, Object.assign throws TypeError when the target is undefined or null. The polyfill re-implements the same behavior for browsers lacking native Object.assign, so calling it with a nullish target fails the same way the builtin would.","triggerScenarios":"Calling Object.assign(undefined, ...) or Object.assign(null, ...) (or a variable intended to be the target object that is nullish) on an old browser/runtime where the polyfill replaces the native method.","commonSituations":"Older browsers (IE11, legacy mobile WebViews) without native Object.assign; a config/defaults object variable that failed to initialize and is undefined/null when merged with defaults.","solutions":["Ensure the target argument is an object before calling Object.assign","Default the target: Object.assign(target || {}, source)","Debug why the variable holding the target is undefined/null","Support modern browsers where native Object.assign is available and fails with the same TypeError, making the root cause the same"],"exampleFix":"// before\nconst config = Object.assign(userOptions, defaults); // userOptions is undefined\n\n// after\nconst config = Object.assign({}, defaults, userOptions || {});","handlingStrategy":"type-guard","validationCode":"function isObjectAssignSafe(target) {\n  return target !== undefined && target !== null;\n}\n\nif (!isObjectAssignSafe(target)) {\n  target = {};\n}\nconst merged = Object.assign(target, defaults);","typeGuard":"function isNonNullObject(v) {\n  return v !== null && typeof v === 'object';\n}\n\nif (!isNonNullObject(userOptions)) userOptions = {};","tryCatchPattern":"let merged;\ntry {\n  merged = Object.assign(userOptions, defaults);\n} catch (e) {\n  if (e instanceof TypeError && /undefined or null/.test(e.message)) {\n    merged = Object.assign({}, defaults);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Always pass an object literal ({}) as the first argument to Object.assign when merging defaults","Coerce possibly-nullish variables with `x || {}` before using them as assign targets","Drop nullish keys from options objects before merging","Prefer spread syntax ({ ...defaults, ...options }) which tolerates nullish sources in modern targets and check target separately"],"tags":["javascript","polyfill","object-assign","browser-compatibility","null"],"backgroundTag":"null-or-undefined-target","analyzedSha":"42343088f22619cf014a057b3878fe3d320016a6","analyzedAt":"2026-09-01T00:27:28.147Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}