Uncaught Typeerror: Cannot read Property 'split' of Undefined

Asked

Viewed 7,151 times

1

Hello!

In my Javascript code I get the following error: "Uncaught Typeerror: Cannot read Property 'split' of Undefined". As I’m new in the area, I don’t quite understand how to solve the problem, someone can help me identify it?

var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var marks = [];
for (var i = 0; i < location.length; i++) {
    marks[i] = createMark(i); /*ERRO*/
}

function createMark() {
    var imagePath = "marker" + (i + 1) + ".png";
    var image = imagePath;
    var markers = [];
    var lat = locations[i].split(',')[0]; /*ERRO*/
    var long = locations[i].split(',')[1];
    var location = new google.maps.LatLng(lat, long);
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        icon: image
    });
    marker.setTitle("Cliente");
    var textOrder = "<strong> Ponto: </strong> " + (i + 1) + ". <br> <strong> Latitude: </strong>" + lat + ", <strong> Longitude: </strong>" + long;
    var infowindow = new google.maps.InfoWindow({
        content: textOrder
    });
    google.maps.event.addListener(marker, "click", function() {
        infowindow.open(map, marker);
    });
}
  • 3

    Forgot to give arguments to the function function createMark()... I think you want to function createMark(i). But there are more things, this function returns nothing, so marks[i] will give undefined.

  • I thank Sergio for the help! But previously this code worked, only now began to return me errors... If it’s not abuse on my part, would you tell me what kinds of arguments I should pass to Mark[i]?

  • 1

    At the end of the joint function return marker; Thus the intância of the marker is stored, in the respective mark[i].

  • Sergio, thanks again! It still didn’t work... I will post the full code...

  • 2

    Can you make a working example? (that shows the error and has HTML). I think you will need these 3 amendments: https://paste.ofcode.org/33TFGAbVx5D5EjsVapDMgqd

  • 1

    It worked perfectly Sergio!!! Thanks!!! -D

Show 1 more comment

2 answers

2

On the line where the error is occurring you pass the variable i as parameter to Function createMark(), but it turns out that this Function is not expecting any parameter:

for (var i = 0; i < location.length; i++) {
    marks[i] = createMark(i); /*ERRO*/
}

What should be done is to change the line

function createMark() {

To

function createMark(i) {
  • It worked!! Thank you!!!

2


3 things to fix:

function createMark(i) { // <--------------- mudança 1
    var imagePath = "marker" + (i + 1) + ".png";
    var image = imagePath;
    var markers = [];
    if (!locations[i]) return null; // <--------------- mudança 2
    var lat = locations[i].split(',')[0]; /*ERRO*/
    var long = locations[i].split(',')[1];
    var location = new google.maps.LatLng(lat, long);
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        icon: image
    });
    marker.setTitle("Cliente");
    var textOrder = "<strong> Ponto: </strong> " + (i + 1) + ". <br> <strong> Latitude: </strong>" + lat + ", <strong> Longitude: </strong>" + long;
    var infowindow = new google.maps.InfoWindow({
        content: textOrder
    });
    google.maps.event.addListener(marker, "click", function() {
        infowindow.open(map, marker);
    });
    return marker; // <--------------- mudança 3
}

The first is the lack of i within the scope of the function.
The second is a safeguard case locations[i] do not have what is expected.
The third is for the function to return something to be stored in marks[i]

  • I have one more question on the subject, can you help me? If you can call me in chat, I would really appreciate it. Thank you!!!!

  • @Z.. you can write here. When you have more points you can write in the chat as well

  • OK @Sergio!! The question is in answer to this question...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.