我正在使用谷歌地图自动完成,我正在检索这样一个字段中的每个对象:
$("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
$("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
$("#usp-custom-23").val(arrAddress.filter(x => x.types.includes("locality"))[0].long_name);
$("#usp-custom-8").attr("value", arrAddress.filter(x => x.types.includes("country"))[0].long_name);
$("#usp-custom-25").val(arrAddress.filter(x => x.types.includes("postal_code"))[0].long_name);
但有时我们没有这些字段的一部分或全部,我们会出现这样的错误:
未捕获类型错误:无法读取未定义的属性"long_name">
我试图通过检查这些字段是否存在来进行条件检查,我尝试了检查undefined
或简单地检查它是否存在:
if(arrAddress.filter(x => x.types.includes("route"))[0] != "undefined") {
$("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name){
$("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
}
但我仍然得到:
未捕获类型错误:无法读取未定义的属性"long_name">
它指的是if
内的string long_name
完整代码:
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
}
marker.setPosition(place.geometry.location);
currentLatitude = place.geometry.location.lat();
currentLongitude = place.geometry.location.lng();
var arrAddress = place.address_components;
var Newlat = map.getCenter().lat();
var NewLong = map.getCenter().lng();
$("#usp-custom-19").val(parseInt(Newlat));
$("#usp-custom-20").val(parseInt(NewLong));
if(arrAddress.filter(x => x.types.includes("route"))[0] != "undefined") {
$("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name){
$("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("locality"))[0].long_name) {
$("#usp-custom-23").val(arrAddress.filter(x => x.types.includes("locality"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("country"))[0].long_name) {
$("#usp-custom-8").val(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("postal_code"))[0].long_name) {
$("#usp-custom-25").val(arrAddress.filter(x => x.types.includes("postal_code"))[0].long_name);
}
if(place.formatted_address) {
$("#usp-custom-60").val(place.formatted_address);
}
$("#usp-custom-90").val(Newlat+","+NewLong);
console.log(place.formatted_address);
});
试试这个:
if(arrAddress.filter(x => x.types.includes("route"))[0] != undefined) {
$("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("street_number"))[0] != undefined){
$("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
}
你试过这个吗?
if(arrAddress.filter(x => x.types.includes("route"))[0].long_name != "undefined") {
$("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
}
您可能还需要检查NaN或null类型。使用typeof计算失败时的类型:
console.log(typeof x.types.includes("route"))[0])
或
console.log(typeof x.types.includes("route"))[0].long_name)