0

I'm pasting the entire stack trace

D:\Data\Documents\iBusFunctions\functions\objects.js:86
        geohash = GeoHasher.encodeGeoHash({latitude: this.stop_lat,longitude: this.stop_lon});

TypeError: Cannot read property 'encodeGeoHash' of undefined
    at Stop.createGeohash (D:\Data\Documents\iBusFunctions\functions\objects.js:86:29)
    at D:\Data\Documents\iBusFunctions\functions\geohasher.js:72:35
    at Array.forEach (<anonymous>)
    at GeoHasher.init (D:\Data\Documents\iBusFunctions\functions\geohasher.js:63:24)
    at Object.<anonymous> (D:\Data\Documents\iBusFunctions\functions\testGeohasher.js:23:4)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

The function in question is defined in objects.js inside the class Stop like this (omitting the constructor because it's kinda big):

class Stop {

    constructor(stop_id, stop_code, stop_name, stop_desc, stop_lat, stop_lon,zone_id, stop_url, location_type, parent_station, stop_timezone, wheelchair_boarding, level_id, platform_code){

       // Omitted

    }

    createGeohash(){
        this.geohash = GeoHasher.encodeGeoHash({latitude: this.stop_lat,longitude: this.stop_lon});
    }

 }

As shown by the code the encodeGeoHash function comes from another module which is imported at the beginning of objects.js like this:

const {GeoHasher} = require('./geohasher');

Finally geohasher.js looks like this:

const Objects = require('./objects');

//Omitted

class GeoHasher {

//Omitted

    static encodeGeoHash(geopoint) {
        var is_even = 1;
        var lat = [];
        var lon = [];
        var bit = 0;
        var ch = 0;
        var precision = 12;
        var geohash = "";

        lat[0] = -90.0;
        lat[1] = 90.0;
        lon[0] = -180.0;
        lon[1] = 180.0;

        while (geohash.length < precision) {
            if (is_even) {
                let mid = (lon[0] + lon[1]) / 2;
                if (geopoint.longitude > mid) {
                    ch |= BITS[bit];
                    lon[0] = mid;
                } else
                    lon[1] = mid;
            } else {
                let mid = (lat[0] + lat[1]) / 2;
                if (geopoint.latitude > mid) {
                    ch |= BITS[bit];
                    lat[0] = mid;
                } else
                    lat[1] = mid;
            }

            is_even = !is_even;
            if (bit < 4)
                bit++;
            else {
                geohash += BASE32[ch];
                bit = 0;
                ch = 0;
            }
        }
        return geohash;
    }

  //Omitted

  }

exports.GeoHasher = GeoHasher;

I omitted some imports and declaration, as well as the constructor for GeoHasher and other functions. The class is working perfectly fine. I've run tests for it and all, and in all of them, encodeGeoHash works perfectly fine. Is it because I'm importing objects.js in GeoHasher? I'm adding what I'm getting from the breakpoint, which only confirms the stacktrace error.

Highlights at Breakpoint VSCode

Damian Dziaduch
  • 2,107
  • 1
  • 15
  • 16
WardoPo
  • 1
  • 4
  • You do have a circular dependency defined, some interesting strategies how to deal with them are available at: https://stackoverflow.com/q/10869276/9240674 – mottek Feb 03 '21 at 22:44

1 Answers1

-1

I had the same error message when trying to access a static member.

// MyClass.js
export class MyClass {
    static staticFunc(){
        return "foo";
    }
}

Then accessing MyClass.staticFunc() gave exactly the same error: Cannot read property of undefined.

For me it was due to importing the class incorrectly:

incorrect:

import MyClass from "MyClass";

correct:

import {MyClass} from "MyClass";
henon
  • 2,022
  • 21
  • 23