Skip to content
engineering workbench.by StudyPort Academy
All errors
data · DEBUG LIBRARY

Cannot convert undefined or null to object

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

An operation that needs an was given or . Object.keys, Object.values, and Object.entries perform this conversion and reject those two values. A nullable API field or missing function argument is a common source.

Check the operation actually used before applying a fix. Modern spread, such as {...} or {...}, does not throw this error; it contributes no properties. Array spread has a different requirement: its value must be iterable, so spreading null there produces an iterable-related TypeError.

The useful question is whether missing data is allowed by your contract. Treating a required record as an empty may remove the crash while silently losing important information.

Minimal reproduction

Keys of an absent record

const settings = null;
console.log(Object.keys(settings));

Entries from a missing argument

function listSettings(settings) {
  return Object.entries(settings);
}
listSettings();

The fix

Guard the nullable value before calling utilities. If absence intentionally means “no entries,” a default to {} is concise and preserves other non- values for further validation.

If a record is required, validate its type and report the missing or malformed value. Do not rely on typeof alone to exclude , because typeof null is . For a record-specific contract you may also need to reject .

An intentional empty-record fallback

const settings = null;
const keys = Object.keys(settings ?? {});
console.log(keys);

Validate a required record

function listSettings(settings) {
  if (settings === null || typeof settings !== "object" || Array.isArray(settings)) {
    throw new Error("Expected a settings record");
  }
  return Object.entries(settings);
}
console.log(listSettings({ theme: "dark" }));