简体   繁体   English

如何解决这个“Uncaught TypeError:无法将undefined或null转换为object”

[英]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. 请帮忙。

Object.keys(value) returns an array and to check if it is undefined or empty do this Object.keys(value)返回一个数组并检查它是否未定义或为空执行此操作

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

如果您正在使用扩展PureComponent React类,那么请确保将您的状态放在类字段state = {...} ,而不是State类型定义中, type State = {...} ,否则它将是错误,因为它实际上是null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 Uncaught TypeError: Cannot convert null or undefined to object? - How do I resolve a Uncaught TypeError: Cannot convert null or undefined to object? 未捕获的TypeError:无法将未定义或null转换为对象 - Uncaught TypeError: Cannot convert undefined or null to object 如何解决Uncaught TypeError:无法将undefined或null转换为对象 - how to solve Uncaught TypeError: Cannot convert undefined or null to object 如何解决 TypeError:无法将 undefined 或 null 转换为对象 - How to resolve TypeError: Cannot convert undefined or null to object json数据错误未捕获的TypeError:无法将未定义或null转换为对象 - json data error Uncaught TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为 object 反应 - Uncaught TypeError: Cannot convert undefined or null to object React 未捕获的类型错误:无法在推送时将 undefined 或 null 转换为对象(<anonymous> ) - Uncaught TypeError: Cannot convert undefined or null to object at push (<anonymous>) 未捕获的类型错误:无法将未定义或 null 转换为 object React JS - Uncaught TypeError: Cannot convert undefined or null to object React JS JavaScript 未捕获类型错误:无法将未定义或 null 转换为 object - JavaScript Uncaught TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为对象 - Uncaught TypeError: Cannot convert undefined or null to objec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM