2

In populate() function of service.js, I was trying to build an object array to store the passengers' name, house, placemark and marker. Basically, I created a global array, say database[], then when iterating through every passenger's information in PASSENGERS[],I add both the property and value dynamically:

    database[i].name = PASSENGERS[i].name;
    database[i].house = PASSENGERS[i].house;
    database[i].placemark = placemark;
    database[i].marker = marker;

When I validate the code using 'Inspect Elements' on Google, it pops out the message:

Uncaught TypeError:Cannot set property 'name' of undefined. 

Is that the correct way to declare an object array? Or is there anything wrong with adding new property and values? Much appreciated.

1 Answer 1

4

I'm not sure you can simply initialise the new array like that.

Why not just add the placemark and marker to the PASSENGERS array? You can just do that 'on the fly' with PASSENGERS[i].placemark = placemark; etc. Anything you want to store in the passengers array can be done like that. For example, I have a field that holds whether or not the passenger is eligible for a ride.

4
  • That works, thanks mate. But then I tried to store both PASSENGERS[i].name and PASSENGERS[i].house in the shuttle.seat[] array when picking up the passengers. I run into similar problem:
    – Shun
    Commented Jul 9, 2014 at 4:07
  • That works, thanks mate. But then I tried to store both PASSENGERS[i].name and PASSENGERS[i].house in the shuttle.seat[] array when picking up the passengers. I ran into similar problem: it works well if I just code in: shuttle.seats[i] = PASSENGERS[i].name, but it won't store the house information. I tried: shuttle.seats[i].name = PASSENGERS[i].name; shuttle.seats[i].house = PASSENGERS[i].house . The error occurred saying "seats[i] is null, can't do it". Then I tried: shuttle.seats[i].push(name: PASSENGERS[i].name, house: PASSENGERS[i].house), but even worse, google earth won't load!
    – Shun
    Commented Jul 9, 2014 at 4:14
  • shuttle.seats[] can just 'reference' the PASSENGERS. I have shuttle.seats[j] = PASSENGERS[i]; so the shuttle.seat array is holding the entire passenger object. Then I just adjust the values in the PASSENGERS[i] array.
    – curiouskiwi
    Commented Jul 9, 2014 at 4:31
  • Oh, I see. Thanks.
    – Shun
    Commented Jul 10, 2014 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .