[英]Cannot convert undefined or null to object js
我正在使用 static.js 文件,只要所有键都有值,此代码就可以工作。 我想输入来自 url 的数据。 但是,我对“未定义”和“空”值的错误处理没有成功。控制台中的错误显示:
main.js:14 Uncaught TypeError: Cannot convert undefined or null to object at Function.entries () at SVGPathElement. (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)
下面是没有错误处理的 main.js 代码。 我一直在阅读并尝试实施有关此主题的一些建议。 但是,没有任何工作。 我的尝试之一是下面的第二个代码框。 任何建议,将不胜感激。
$("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;
}
```
第二个文件第 14 行的循环是您的错误的来源,并且可能可以被删除(因为我没有看到您想要迭代的任何其他 object):
// this block can likely be removed
Object.entries(undefined).forEach(([key]) => {
if (key != 'undefined' | 'null') {
county_html += `$(0): $(0)<br>`;
}
})
相反,您似乎想检查您的county
object 是否有未定义或 null 值。 这可以在您遵循不必要的循环的循环中完成:
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.