FrontEnd Expert
FrontEnd Expert

Reputation: 5803

TypeError : Cannot convert undefined or null to object javascript

I am getting one type error with my JSON, I am constructing a tree based on json, My JSON structure in not static fields and key values are dynamic,

When I have JSON data -

const data = [
  
  {
    "class": "abc",
    magList: "",
    authKeyMgmtMode: {
      "class": "OctetString",
      octets: "00"
    },
    wsEnable: true,
    wlans: true,
    wsle: true,
    busey: "567ff7c5[180188_180188,temp-ssid_Global_NF_e06779e9]",
    sspe: 0
  }
];

It works fine below is sandbox - Code sandbox - https://codesandbox.io/s/flamboyant-blackwell-dw5yn?file=/index.js

When I add one more value as

Ipv6Name: null,

In json array it's giving me error,

Error - Cannot convert undefined or null to object

Object.keys(object).map((key, reactKey) => {

I have checked the references for same error, but not able to proceed. Please guide me.

Edited -

I noticed I was sending null to Object.keys but I was doing check in isPrimative function I was using the check but there were typo and few logical mistake, I was checking typeof value === null , Thanks for pointing out...

Upvotes: 1

Views: 497

Answers (1)

Iosif
Iosif

Reputation: 822

Use this code because type of null is object:

isPrimative = (value) => {
    return (
      typeof value === "string" ||
      typeof value === "" ||
      typeof value === "number" ||
      typeof value === "boolean" ||
      value === null
    );
  };

Upvotes: 1

Related Questions