Cannot read property "text"of undefined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • belz
    New Member
    • Feb 2014
    • 5

    Cannot read property "text"of undefined

    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


    Code:
    function calculateDistanceCallback(response, status) {
           	     
           		  if (status != google.maps.DistanceMatrixStatus.OK) {
           		    alert('Error was: ' + status);
           		  } else {
           			  alert("in else");
           		    var origins = response.originAddresses;
           		    var destinations = response.destinationAddresses; 
           		    var outputDiv = document.getElementById('outputDiv');
           		    
           		    outputDiv.innerHTML = '';
           		    //deleteOverlays();
           		    for (var i = 0; i < origins.length; i++) {
           		    	alert("in forloop");
           		      var results = response.rows[i].elements;
           		      alert("results" +results);
           		      alert("in second for loop");
           		      for (var j = 0; j < results.length; j++) {
           		    	  alert("insyd second for loop");
           		    	  
           		    	  alert("results" +results[j]);
           		      [B]var a = results[j].distance.text.split(" ")[0];[/B]
           		      
           		    alert("inbtwn for loop");
           		      workingDistance = a;
    
    ///more code here
    Last edited by Dormilich; Feb 27 '14, 02:26 PM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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).

    Comment

    • belz
      New Member
      • Feb 2014
      • 5

      #3
      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

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        there’s another alert() beneath that one, what does it show?

        Comment

        • belz
          New Member
          • Feb 2014
          • 5

          #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

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            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.

            Comment

            • belz
              New Member
              • Feb 2014
              • 5

              #7
              it does produce object Object.then doesnt proceed

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                then it doesn’t have—as I already said—a distance property.

                Comment

                Working...