0

My ajax request for a geojson works fine as a static web app

  $.getJSON("myfile.geojson", function (data) {
    theaters.addData(data);
    map.addLayer(theaterLayer);
  });

I am trying to wrap the project now around flask. All of my js/geojson files are in my static folder.

However when I try to pass static folder url to geojson inside flask (with map.js also residing in static/js) like such:

  $.getJSON("/static/js/myfile.geojson", function (data) {
    theaters.addData(data);
    map.addLayer(theaterLayer);
  });

I get 'Cannot read property 'addLayer' of undefined'.

This is the only file I load from the js. What is the proper way of referencing local files inside my map.js in a flask environment?

1

If you're running this at localhost, you can find where the file is by pasting the path into your browser, like: http://localhost/static/js/myfile.geojson.

If that works, then your JS needs to change. If it doesn't, then you need to change your path.

You can perhaps check what the path should be by looking at your web browser's console and watching the network traffic. What is the path for javascript files?


Take two, actually, look at the error!

'Cannot read property 'addLayer' of undefined'.

This means that the map object is not defined, probably just in this scope.

You might try using an arrow function (which passes in the parent scope), like this:

$.getJSON("/static/js/myfile.geojson", (data) => {
    theaters.addData(data);
    map.addLayer(theaterLayer);
});

If that's not it, maybe start using console.log to find out whether data is an object with your data, and if map is referring to your actual map object.

  • localhost:5000/static/js/myfile.geojson downloads the file. So the .getJSON("/static/js/myfile.geojson",... path is correct. The map.js is in the same folder ex. - localhost:5000/static/js/map.js. It seems to be something in the way flask references files? – wtgeographer Dec 20 '17 at 1:50
  • Hey @wtgeographer I note that your error message is saying that your map is not defined. Can you check you've initialised it? I'll add more in my answer. – Alex Leith Dec 21 '17 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.