Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
66 views
in Technique[技术] by (71.8m points)

javascript - Cannot read property of undefined. But property is a static function of an imported class

I'm pasting the entire stack trace

D:DataDocumentsiBusFunctionsfunctionsobjects.js:86
        geohash = GeoHasher.encodeGeoHash({latitude: this.stop_lat,longitude: this.stop_lon});

TypeError: Cannot read property 'encodeGeoHash' of undefined
    at Stop.createGeohash (D:DataDocumentsiBusFunctionsfunctionsobjects.js:86:29)
    at D:DataDocumentsiBusFunctionsfunctionsgeohasher.js:72:35
    at Array.forEach (<anonymous>)
    at GeoHasher.init (D:DataDocumentsiBusFunctionsfunctionsgeohasher.js:63:24)
    at Object.<anonymous> (D:DataDocumentsiBusFunctionsfunctionsestGeohasher.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

question from:https://stackoverflow.com/questions/65832014/cannot-read-property-of-undefined-but-property-is-a-static-function-of-an-impor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...