Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 661
Pooja Mokariya Avatar asked Aug 29 '16 06:08

Pooja Mokariya


2 Answers

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 
    }
like image 59
Amar Singh Avatar answered Nov 18 '22 16:11

Amar Singh


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.

like image 44
Rock Lee Avatar answered Nov 18 '22 16:11

Rock Lee