简体   繁体   English

无法将未定义或 null 转换为 object js

[英]Cannot convert undefined or null to object js

I was working with a static.js file and this code worked as long as all keys had a value.我正在使用 static.js 文件,只要所有键都有值,此代码就可以工作。 I would like to feed in the data from an url.我想输入来自 url 的数据。 However, I have been unsuccessful in error handling for the "undefined" and "null" values". The error in the console reads:但是,我对“未定义”和“空”值的错误处理没有成功。控制台中的错误显示:

main.js:14 Uncaught TypeError: Cannot convert undefined or null to object at Function.entries () at SVGPathElement. main.js:14 Uncaught TypeError: Cannot convert undefined or null to object at Function.entries () at SVGPathElement. (main.js:14) at SVGPathElement.handle (jquery-2.2.4.min.js:3) at SVGPathElement.dispatch (jquery-2.2.4.min.js:3) at SVGPathElement.r.handle (jquery-2.2.4.min.js:3) (main.js:14) 在 SVGPathElement.handle (jquery-2.2.4.min.js:3) 在 SVGPathElement.dispatch (jquery-2.2.4.min.js:3) 在 SVGPathElement.r.handle (jquery- 2.2.4.min.js:3)

Below is the main.js code without error handling.下面是没有错误处理的 main.js 代码。 I have been reading and trying to implement some of the suggestions on this topic.我一直在阅读并尝试实施有关此主题的一些建议。 However, nothings working.但是,没有任何工作。 One of my attempts is the second code box below.我的尝试之一是下面的第二个代码框。 Any suggestions would be appreciated.任何建议,将不胜感激。


        $("path, circle").hover(function(e) {
          // make tooltip visible
          $('#info-box').css('display','block');
          // get year from selector element
          const year = document.querySelector('#myList').value;
          // filter the `data` array for states just in that year
          const filtered = data.filter(d => d.Year == year);
          // filter states of that year to just the one state matching the id of 
          // the path that is being hovered on 
          const state = filtered.filter(d => d.id == $(this).attr('id'))[0];
          // create the html string to populate the tooltip with 
          // as long as the key isn't 'id' then continue building
          let state_html = '';
          Object.entries(state).forEach(([key, value]) => {
            if (key != 'id') {
            state_html += `${key}: ${value}<br>`;
            }
          })
          // change value of tooltip to html we just made
          $('#info-box').html(state_html);
        });
        $("path, circle").mouseleave(function(e) {
          $('#info-box').css('display','none');
        });
        $(document).mousemove(function(e) {
          $('#info-box').css('top',e.pageY-$('#info-box').height()-30);
          $('#info-box').css('left',e.pageX-($('#info-box').width())/2);
        }).mouseover();
        var ios = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
        if(ios) {
          $('a').on('click touchend', function() {
            var link = $(this).attr('href');
            window.open(link,'_blank');
            return false;
          });
        }
        function getOption() {
          const selectElement = document.querySelector('#myList');
          output = selectElement.value;
          document.querySelector('.output').textContent = output;
        }

    $("path, polyline, polygon").hover(function(e) {
      // make tooltip visible
      $('#info-box').css('display','block');
      // get date from selector element
      const Date = document.querySelector('#myList').value;
      // filter the `data` array for counties just in that date
      const filtered = data.filter(d => d.date == Date);
      // filter counties of that date to just the one county matching the id of 
      // the path that is being hovered on 
      const county = filtered.filter(d => d.id == $(this).attr('id'))[0];
      // create the html string to populate the tooltip with 
      // as long as the key isn't 'id' then continue building
      let county_html = '';
      Object.entries(undefined).forEach(([key]) => {
      if (key != 'undefined' | 'null') {
        county_html += `$(0): $(0)<br>`;
        }
      })
      Object.entries(county).forEach(([key, value]) => {
        if (key != 'id') {
          county_html += `${key}: ${value}<br>`;
        }
      })
      // change value of tooltip to html we just made
      $('#info-box').html(county_html);
    });
    $("path, polyline, polygon").mouseleave(function(e) {
      $('#info-box').css('display','none');
    });
    $(document).mousemove(function(e) {
      $('#info-box').css('top',e.pageY-$('#info-box').height()-30);
      $('#info-box').css('left',e.pageX-($('#info-box').width())/2);
    }).mouseover();
    var ios = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
    if(ios) {
      $('a').on('click touchend', function() {
        var link = $(this).attr('href');
        window.open(link,'_blank');
        return false;
      });
    }
    function getOption() {
      const selectElement = document.querySelector('#myList');
      output = selectElement.value;
      document.querySelector('.output').textContent = output;
    }

```

The loop on line 14 of the second file is the source of your error, and probably can just be removed (as I don't see any other object you would want to iterate through):第二个文件第 14 行的循环是您的错误的来源,并且可能可以被删除(因为我没有看到您想要迭代的任何其他 object):

      // this block can likely be removed
      Object.entries(undefined).forEach(([key]) => {
        if (key != 'undefined' | 'null') {
          county_html += `$(0): $(0)<br>`;
        }
      })

Instead, it looks like you want to check your county object for undefined or null values.相反,您似乎想检查您的county object 是否有未定义或 null 值。 That can be done in the loop you have following the unnecessary one:这可以在您遵循不必要的循环的循环中完成:

Object.entries(county).forEach(([key, value]) => {
  if (key !== 'id' && key !== undefined && key !== null) {
    county_html += `${key}: ${value}<br>`;
  }
});

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

相关问题 无法将未定义或null转换为对象 - Cannot convert undefined or null to object React JS:TypeError:无法将未定义或空值转换为对象 - React JS: TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为 object React JS - Uncaught TypeError: Cannot convert undefined or null to object React JS 渲染错误:“TypeError:无法在 vue js 中将未定义或 null 转换为对象”? - Error in render: “TypeError: Cannot convert undefined or null to object” in vue js? Draft.js无法将未定义或null转换为对象convertFromRaw - Draft.js Cannot convert undefined or null to object convertFromRaw Google Cloud Function JS错误:无法将undefined或null转换为object - Google Cloud Function JS Error: Cannot convert undefined or null to object 无法将未定义或 null 转换为 Next.js 中的 object - Cannot convert undefined or null to object in Next.js 伴侣-bubble.js:无法将未定义或 null 转换为 object - companion-bubble.js: Cannot convert undefined or null to object JS setter 返回 ''undefined'' 并且 getter 导致 Uncaught TypeError TypeError: Cannot convert undefined or null to object - JS setters return ''undefined'' and the getter causes Uncaught TypeError TypeError: Cannot convert undefined or null to object 无法将未定义或null转换为React中的对象 - Cannot convert undefined or null to object in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM