"Uncaught TypeError: Cannot read property 'length' of undefined" after defining var
"Uncaught TypeError: Cannot read property 'length' of undefined" after defining var
我目前正在加载一个 json 文件,然后对其进行解析。 json文件中的条目存储为"places",我将其定义为全局变量,但浏览器仍然说它未定义。
var request;
var places;
var map;
var myLatLng = {lat: 34, lng: 38};
// load database and parse into entries
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'places.json');
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
}
}
request.send();
function initMap() {
var mapOptions = {
zoom: 6,
center: myLatLng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
mapTypeId: google.maps.MapTypeId.HYBRID
};
// initialize the map
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
for (var i = 0; i < places.length; i++) {
// the place
var place = places[i];
// place co-ordinates
var latlng = new google.maps.LatLng(place.latitude, place.longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: place.city
});
}
}
Jonathan Lonowski 和 Shashank 提供的评论最有可能导致您遇到的问题。您需要确保在 AJAX 调用完成后调用 initMap
。因此,您可以将 onreadystatechanged
方法修改为如下所示:
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
initMap();
}
不过,我还想用 Javascript 的说法澄清 undefined
的概念。
在 Javascript 中,当变量被声明(但没有赋值)时,它被设置为 undefined
的值。请注意,undefined
与 null
不同, 更像是一个赋值。
This 答案将澄清 undefined
和 null
之间的混淆。然后您将能够理解为什么浏览器将变量位置显示为 undefined
.
根据 OP 的评论进行编辑 如果您从 HTML 文件中调用该方法,仍然有一种方法可以使页面正常运行,但是一些可以忽略不计的延迟。
参考我在下面提出的替代解决方案
var request;
var places;
var map;
var myLatLng = {lat: 34, lng: 38};
// load database and parse into entries
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'places.json');
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
//now that the places array has been initialized you can call
//placeMarkers() to place markers on the map.
placeMarkers();
}
}
request.send();
//initMap can be safely invoked within the HTML page using window.onload.
//this only initializes the map instance and sets it to a valid reference.
//there are *no* place markers added yet
function initMap() {
var mapOptions = {
zoom: 6,
center: myLatLng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
mapTypeId: google.maps.MapTypeId.HYBRID
};
// initialize the map. Here map should be the global variable and not a new declaration
map = new google.maps.Map(document.getElementById('map'), mapOptions);
}
function placeMarkers(){
for (var i = 0; i < places.length; i++) {
// the place
var place = places[i];
// place co-ordinates
var latlng = new google.maps.LatLng(place.latitude, place.longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: place.city
});
}
}
我目前正在加载一个 json 文件,然后对其进行解析。 json文件中的条目存储为"places",我将其定义为全局变量,但浏览器仍然说它未定义。
var request;
var places;
var map;
var myLatLng = {lat: 34, lng: 38};
// load database and parse into entries
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'places.json');
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
}
}
request.send();
function initMap() {
var mapOptions = {
zoom: 6,
center: myLatLng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
mapTypeId: google.maps.MapTypeId.HYBRID
};
// initialize the map
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
for (var i = 0; i < places.length; i++) {
// the place
var place = places[i];
// place co-ordinates
var latlng = new google.maps.LatLng(place.latitude, place.longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: place.city
});
}
}
Jonathan Lonowski 和 Shashank 提供的评论最有可能导致您遇到的问题。您需要确保在 AJAX 调用完成后调用 initMap
。因此,您可以将 onreadystatechanged
方法修改为如下所示:
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
initMap();
}
不过,我还想用 Javascript 的说法澄清 undefined
的概念。
在 Javascript 中,当变量被声明(但没有赋值)时,它被设置为 undefined
的值。请注意,undefined
与 null
不同, 更像是一个赋值。
This 答案将澄清 undefined
和 null
之间的混淆。然后您将能够理解为什么浏览器将变量位置显示为 undefined
.
根据 OP 的评论进行编辑 如果您从 HTML 文件中调用该方法,仍然有一种方法可以使页面正常运行,但是一些可以忽略不计的延迟。
参考我在下面提出的替代解决方案
var request;
var places;
var map;
var myLatLng = {lat: 34, lng: 38};
// load database and parse into entries
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'places.json');
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
//now that the places array has been initialized you can call
//placeMarkers() to place markers on the map.
placeMarkers();
}
}
request.send();
//initMap can be safely invoked within the HTML page using window.onload.
//this only initializes the map instance and sets it to a valid reference.
//there are *no* place markers added yet
function initMap() {
var mapOptions = {
zoom: 6,
center: myLatLng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
mapTypeId: google.maps.MapTypeId.HYBRID
};
// initialize the map. Here map should be the global variable and not a new declaration
map = new google.maps.Map(document.getElementById('map'), mapOptions);
}
function placeMarkers(){
for (var i = 0; i < places.length; i++) {
// the place
var place = places[i];
// place co-ordinates
var latlng = new google.maps.LatLng(place.latitude, place.longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: place.city
});
}
}