Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this is bound to functions, need to make that a arrow function #2

Open
oehm-smith opened this issue May 15, 2024 · 3 comments
Open

Comments

@oehm-smith
Copy link

oehm-smith commented May 15, 2024

This code fails:

if (Array.isArray(objects)) {
      geojson = { type: "FeatureCollection", features: [] };
      objects.forEach(function(item) {
        geojson.features.push(
          this.getFeature({ item: item, params: settings, propFunc: propFunc })
        );
      });
      this.addOptionals(geojson, settings);
    } else {
      geojson = this.getFeature({

this inside functions is bound to the function.

It needs to rewritten with arrow function so that this is bound to the original object:

    if (Array.isArray(objects)) {
      geojson = { type: "FeatureCollection", features: [] };
      objects.forEach((item) => {
        geojson.features.push(
          this.getFeature({ item: item, params: settings, propFunc: propFunc })
        );
      });
      this.addOptionals(geojson, settings);
    } else {

It's also good to use let or const instead of var since the latter has global scope.

Thanks for writing this as I too struck the problem of the geojson .js project not having @types. I think the better path would have been to insist https://github.com/caseycesari/GeoJSON.js rename their project and create a .dt. You seem to know them and perhaps you could have this conversation? :)

@eugeneYWang
Copy link
Owner

Thank you for this suggestion! I would find sometime to solve this.

Casey the original author have not been active in that repo for quote some years. I don't know if she would like to spend time to work with those orgs to change its name.

@eugeneYWang
Copy link
Owner

I wonder if you have tested this change in your own project?

It looks great but since it worked with me before I am wondering why it cause your concern here.

@echelon35
Copy link

echelon35 commented Oct 4, 2024

Hi, I had the same problem.
I tried to make a FeatureGroup with multiple LineString.
I had this error :
Cannot read properties of undefined (reading 'getFeature')

I've just add this context inside the foreach loop because it was not bound to the original object :
//Line 58 of geojson.ts
objects.forEach(function(item) { geojson.features.push( this.getFeature({ item: item, params: settings, propFunc: propFunc }) ); }, this);

It works fine now :
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [ -34.7, 10.5 ], [ -36.1, 11 ] ] }, "properties": { "type": "LineString" } }, ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants