How to resolve this " Uncaught TypeError: Cannot convert undefined or null to object "

47,056

Solution 1

Object.keys(value) returns an array and to check if it is undefined or empty do this

 if (typeof Object.keys(value) !== 'undefined' && Object.keys(value).length > 0) {
        // You have an array 
    }

Solution 2

If you are using React classes that extend PureComponent, then make sure you put your state in the class field, state = {...}, not the State type definition, type State = {...}, or else it will be an error because it actually was null.

Share:
47,056
Pooja Mokariya
Author by

Pooja Mokariya

A Computer Engineer Graduated from Nirma University. Currently working as a Software Developer on Ruby On Rails Technology. Having a strong passion and strength to work as a Software Developer with Ruby on Rails Technology. Result oriented, self-learner, eager to learn new technologies, methodologies, strategies and processes

Updated on August 03, 2022

Comments

  • Pooja Mokariya
    Pooja Mokariya almost 2 years

    My function is :

    function collect_que_ids(e) {
      var val = e.val();
      var data_lo = e.attr('data-lo');
      new_hash = {};
      new_hash[val] = data_lo;
      if(e.is(':checked')){
        if(checked_box_hash.includes(new_hash)){
          checked_box_hash;
        }else{
          checked_box_hash.push(new_hash);
        }
      }
      else{
        new_hash_key = Object.keys(new_hash)[0]
        new_hash_value = new_hash[new_hash_key]
        $.each(checked_box_hash, function(key, value){
          if (typeof Object.keys(value) !== 'nil') {
            current_key = Object.keys(value)
            if (current_key[0] == new_hash_key && value[current_key[0]] == new_hash_value) {
              checked_box_hash.splice(key, 1)
            }
          }
        })
      }
    };
    

    I am getting error on this line.

    if (typeof Object.keys(value) !== 'nil') {
    

    Need to resolve it. please help to do.