2

So I have been using the geolocation to find the user location and then find the distance to it and a csv list of locations. I want to save the distance to the json object, but can't access it in my nested function.

function onLocationFound(e) {
    console.log(e);
    L.marker([e.latitude, e.longitude], {icon: home_marker}).bindPopup("You are here!").addTo(m);
    console.log(typeof(e.latitude));
    console.log(typeof(e.longitude));
    $.get('vac_sites.csv', function (csvString) {

        var data = Papa.parse(csvString, { header: true, dynamicTyping: true }).data;
        console.log(data)
        for (let i = 0; i < data.length; i++) {
        //get distance and then use dynamic variable names, make them into js objects, then store in dic with distance
          var row = data[i];
          console.log(row)
          var myRoute = L.Routing.osrmv1({
            serviceUrl: 'https://xxx.xxx.xxx:443/route/v1'
          });
          var self_loc = new L.Routing.Waypoint;
          self_loc.latLng = L.latLng([e.latitude, e.longitude]);
          var site_loc = new L.Routing.Waypoint;
          site_loc.latLng = L.latLng([row.Latitude, row.Longitude]);
          myRoute.route([self_loc, site_loc], function(err, routes) {
              distance = routes[0].summary.totalDistance;
              console.log('routing distance: ' + distance);
              row.distance = distance
              console.log(row)
          });

When I open the console, it appears to have created a new json object and added row to it. How can I access the original row variable and add the distance to it? Is it a problem with function scope?

EDIT: When I open console I get this for the first console.log(row):

...
{Name: "Cate Pharmacy", Address: "500 N Missouri Ave, Corning, Arkansas", Telephone: "(870) 857-6766", Website: "<a href="https://www.catepharmacy.com/" target="_b…ow noopener noreferrer">www.catepharmacy.com/</a>", Latitude: 36.4155144, …}
...

I want to add a key and value pair for this that is the distance of the route in the form distance: xxxxx. Desired result is:

...
{Name: "Cate Pharmacy", Address: "500 N Missouri Ave, Corning, Arkansas", Telephone: "(870) 857-6766", Website: "<a href="https://www.catepharmacy.com/" target="_b…ow noopener noreferrer">www.catepharmacy.com/</a>", Latitude: 36.4155144, distance: xxxxxx, …}
...

But instead at the second console.log(row) I get this:

{Name: null, distance: 265184.8}
Jimmy
  • 37
  • 5
  • Are you saying that `console.log(row)` doesn't log the expected value? What makes you think you cannot access the variable? – Bergi Apr 16 '21 at 20:39
  • It's not clear from your description, but you might be running into [hoisting](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example). – Raymond Chen Apr 16 '21 at 20:42
  • console.log(row) shows a json object that has Name: null distance: xxxx instead of my original row json object that has facility name, lat, lon, etc. – Jimmy Apr 16 '21 at 20:43
  • It's not clear what you mean by "original row object". I think you mean "the value of `row` during the first iteration of the `for (var i in data)` loop." And the answer is "hoisting". See the linked answer. – Raymond Chen Apr 16 '21 at 21:23
  • I tried the fixes there, but I still encountered the same problem. – Jimmy Apr 16 '21 at 22:00
  • You need to describe the problem better in the question. Apply your fix to the question and then explain what you see and what you expect. The description you give looks a lot like hoisting, and changing `var` to `let` should help. – Raymond Chen Apr 16 '21 at 22:14
  • Changed let to var and still got the same thing...updated question with what I want. – Jimmy Apr 16 '21 at 22:38
  • Does this answer your question? [console.log() shows the changed value of a variable before the value actually changes](https://stackoverflow.com/questions/11284663/console-log-shows-the-changed-value-of-a-variable-before-the-value-actually-ch) – Siguza Apr 17 '21 at 15:27
  • Sorry no. I want to edit the value of the row object by adding a key/value pair, but I can't seem to acces it and I've tried hoisting fixes. – Jimmy Apr 17 '21 at 17:29

1 Answers1

2

Talking on the chat we solved the problem.

It was narrowed down to changing var row; to let row;

It sounds like row is leaked into your function somehow. You should read this to understand the differences between the two variable declaration keywords. In a nutshell, var is bound to the immediate function body while let is bound to the immediate closing block. That could be what caused it. Otherwise, I don't know.

It's best to use let because var is almost always unnecessary and can cause problems.

Reality
  • 637
  • 1
  • 6
  • 25