By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Cannot read property "text"of undefined

P: 5
i have the following function for calculating distances.this function is the distance call back function.it goes throught all the alert statements but when it reaches the line in bold it throws an errorcannot read proprty "text of undefined".Is there anything i did wrong above such that my code cannot pass through this statement


Expand|Select|Wrap|Line Numbers
  1. function calculateDistanceCallback(response, status) {
  2.  
  3.                  if (status != google.maps.DistanceMatrixStatus.OK) {
  4.                    alert('Error was: ' + status);
  5.                  } else {
  6.                      alert("in else");
  7.                    var origins = response.originAddresses;
  8.                    var destinations = response.destinationAddresses; 
  9.                    var outputDiv = document.getElementById('outputDiv');
  10.  
  11.                    outputDiv.innerHTML = '';
  12.                    //deleteOverlays();
  13.                    for (var i = 0; i < origins.length; i++) {
  14.                        alert("in forloop");
  15.                      var results = response.rows[i].elements;
  16.                      alert("results" +results);
  17.                      alert("in second for loop");
  18.                      for (var j = 0; j < results.length; j++) {
  19.                          alert("insyd second for loop");
  20.  
  21.                          alert("results" +results[j]);
  22.                      var a = results[j].distance.text.split(" ")[0];
  23.  
  24.                    alert("inbtwn for loop");
  25.                      workingDistance = a;
  26.  
  27. ///more code here
Feb 27 '14 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
that’s because results[j] is not an object (or does not contain a distance property, but the alert() above pretty much rules that out).
Feb 27 '14 #2

P: 5
My code goes up to the alert which says second for loop.so what should i do to make the code work.can you write the code for me containing the change
Feb 27 '14 #3

Dormilich
Expert Mod 5K+
P: 8,639
there’s another alert() beneath that one, what does it show?
Feb 27 '14 #4

P: 5
hi.am using alerts as debug statements.lyk i said my code goz up to the alert wch says results +resulte[j].it doesnt proceed to go and print out the other alerts.meaning the prroblem is were ere is e distance.text code.bcz dats e only code inbtwn e 2 alert statrnents
Mar 3 '14 #5

Dormilich
Expert Mod 5K+
P: 8,639
lyk i said my code goz up to the alert wch says results +resulte[j]
and if that alert doesn’t show "[object Object]", then post #2 applies—you’re working with the wrong array.
Mar 3 '14 #6

P: 5
it does produce object Object.then doesnt proceed
Mar 3 '14 #7

Dormilich
Expert Mod 5K+
P: 8,639
then it doesn’t have—as I already said—a distance property.
Mar 3 '14 #8

Post your reply

Sign in to post your reply or Sign up for a free account.