最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: g is not a function - Stack Overflow

programmeradmin5浏览0评论

While re-writing one of my applications from scratch (to go from legacy to AMD) I'm encountering an error which I can't figure out. It is driving me crazy. I'm probably just misspelling something or have another minor mistake, but I just can't figure out what. Any help would be highly appreciated!

I'm still in development mode, so my code isn't that pretty at this time. You can see it live in action at here:

The javascript code:

//Define area and url's  
var currentPath = window.location.pathname.split('/');
var AREA = currentPath[currentPath.length - 2];
if (AREA == 'europa' || AREA == 'wereld' || AREA == 'nederland') {
    var AREAURL = AREA;
    var AREAisProvince = false;
} else {
    AREAURL = 'nederland';
    var AREAisProvince = true;
}; //Ondervang provincies  


var basemapURL = window.location.protocol + "//tiles.arcgis/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Topografie_in_de_klas_" + AREAURL + "_ondergrond/MapServer";
var contentFeatureURL = window.location.protocol + "//services.arcgis/nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_" + AREAURL + "/FeatureServer/0";




var CONTENT = 'cito100'; //Default to 'cito100', user can adjust manually  
var TYPES = ''; //empty for now, will be defined later by the user  
var TYPES = 'plaats'; //for testingunction's//  
//  
function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return uuid;
};


//  
//Figure something out here to detect if the user as a new or returning visitor  
//  


//////////////////////////////////////  
//Create a basemap and load features//  
//////////////////////////////////////  
var dojoConfig = { parseOnLoad: true };


var map;
require(["esri/geometry/Extent", "esri/SpatialReference", "esri/map", "esri/graphic", "esri/layers/ArcGISTiledMapServiceLayer", "esri/tasks/query", "esri/tasks/QueryTask", "esri/tasks/FeatureSet", "esri/layers/GraphicsLayer", "esri/Color", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/PictureMarkerSymbol", "esri/renderers/UniqueValueRenderer", "esri/renderers/ClassBreaksRenderer",
    "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dijit/TitlePane",
    "dojo/domReady!", "dojo/dom", "dojo/on"],
    function (Extent, SpatialReference, Map, Graphic, Tiled, Query, QueryTask, FeatureSet, GraphicsLayer, Color, SimpleMarkerSymbol, SimpleLineSymbol, PictureMarkerSymbol, UniqueValueRenderer, ClassBreaksRenderer, dom, on) {
        //@TODO: Can we actually define this on the basemap mapserver?  
        if (AREA == 'nederland' || AREAisProvince == true) {
            var initExtent = new Extent(-165715, 6453119, 1435181, 7205260, new SpatialReference({ wkid: 102100 }));
        }
        if (AREA == 'europa') {
            var initExtent = new Extent(-2827847, 2851709, 6838658, 11375669, new SpatialReference({ wkid: 102100 }));
        }
        if (AREA == 'wereld') {
            var initExtent = new Extent(-19705424, -14849545, 21700207, 21624981, new SpatialReference({ wkid: 102100 }));
        }

        map = new Map("map", {
            extent: initExtent
        });


        //let's add a basemap  
        var tiled = new Tiled(basemapURL);
        map.addLayer(tiled);

        where = 'Cito100_onderdeel=1';
        if (AREAisProvince == true) {
            where += ' AND Provincie=\'' + AREA + '\'';
        }
        map.on("load", getFeaturesToMapAndStorage(where));

        function getFeaturesToMapAndStorage(whereClause) {
            console.log(whereClause);
            //query the featureService  
            var query = new Query();
            query.returnGeometry = true;
            query.outFields = ["*"];
            query.outSpatialReference = new SpatialReference({ wkid: 102100 });
            query.where = whereClause;

            var queryTask = new QueryTask(contentFeatureURL);

            queryTask.on("plete", function (event) {
                //map.graphics.clear();  
                var featureGraphicsLayer = new GraphicsLayer();


                //@TODO: Can't we find a way to use the symbols from the featurservice directly, instead of the url's?  
                defaultSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10,
            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            new Color([255, 0, 0]), 1),
            new Color([0, 255, 0, 0.25]));

                var renderer = new UniqueValueRenderer(defaultSymbol, "Type");
                renderer.addValue("Plaats", new PictureMarkerSymbol("", 11, 11));
                renderer.addValue("Gebied", new PictureMarkerSymbol("", 28, 28));
                renderer.addValue("Water", new PictureMarkerSymbol("", 22, 22));
                renderer.addValue("Provincie", new PictureMarkerSymbol("", 38, 32));
                renderer.addValue("Land", new PictureMarkerSymbol("", 38, 32));
                renderer.addValue("Werelddeel", new PictureMarkerSymbol("", 32, 38));

                var features = event.featureSet.features;
                var featureCount = features.length;
                for (var i = 0; i < featureCount; i++) {
                    var graphic = features[i];
                    featureGraphicsLayer.add(graphic);
                }
                featureGraphicsLayer.renderer = renderer;
                map.addLayer(featureGraphicsLayer);

                //@TODO: Update localstorage    

            }); //end on queryTask plete  

            queryTask.execute(query, queryComplete);

            function queryComplete() {
                console.log("fire function queryComplete");
                //@TODO: reset progressbar  
            }; //End function queryComplete  


        } //end function getFeaturesToMapAndStorage  

        //The two closing tags below are essential and close the plete DOJO part.    
    } //end function after require (AMD style)  
    ); //end require

While re-writing one of my applications from scratch (to go from legacy to AMD) I'm encountering an error which I can't figure out. It is driving me crazy. I'm probably just misspelling something or have another minor mistake, but I just can't figure out what. Any help would be highly appreciated!

I'm still in development mode, so my code isn't that pretty at this time. You can see it live in action at here: http://tpgrf.nl/testserver/alpha/topotrainer/flevoland

The javascript code:

//Define area and url's  
var currentPath = window.location.pathname.split('/');
var AREA = currentPath[currentPath.length - 2];
if (AREA == 'europa' || AREA == 'wereld' || AREA == 'nederland') {
    var AREAURL = AREA;
    var AREAisProvince = false;
} else {
    AREAURL = 'nederland';
    var AREAisProvince = true;
}; //Ondervang provincies  


var basemapURL = window.location.protocol + "//tiles.arcgis./tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Topografie_in_de_klas_" + AREAURL + "_ondergrond/MapServer";
var contentFeatureURL = window.location.protocol + "//services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_" + AREAURL + "/FeatureServer/0";




var CONTENT = 'cito100'; //Default to 'cito100', user can adjust manually  
var TYPES = ''; //empty for now, will be defined later by the user  
var TYPES = 'plaats'; //for testingunction's//  
//  
function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return uuid;
};


//  
//Figure something out here to detect if the user as a new or returning visitor  
//  


//////////////////////////////////////  
//Create a basemap and load features//  
//////////////////////////////////////  
var dojoConfig = { parseOnLoad: true };


var map;
require(["esri/geometry/Extent", "esri/SpatialReference", "esri/map", "esri/graphic", "esri/layers/ArcGISTiledMapServiceLayer", "esri/tasks/query", "esri/tasks/QueryTask", "esri/tasks/FeatureSet", "esri/layers/GraphicsLayer", "esri/Color", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/PictureMarkerSymbol", "esri/renderers/UniqueValueRenderer", "esri/renderers/ClassBreaksRenderer",
    "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dijit/TitlePane",
    "dojo/domReady!", "dojo/dom", "dojo/on"],
    function (Extent, SpatialReference, Map, Graphic, Tiled, Query, QueryTask, FeatureSet, GraphicsLayer, Color, SimpleMarkerSymbol, SimpleLineSymbol, PictureMarkerSymbol, UniqueValueRenderer, ClassBreaksRenderer, dom, on) {
        //@TODO: Can we actually define this on the basemap mapserver?  
        if (AREA == 'nederland' || AREAisProvince == true) {
            var initExtent = new Extent(-165715, 6453119, 1435181, 7205260, new SpatialReference({ wkid: 102100 }));
        }
        if (AREA == 'europa') {
            var initExtent = new Extent(-2827847, 2851709, 6838658, 11375669, new SpatialReference({ wkid: 102100 }));
        }
        if (AREA == 'wereld') {
            var initExtent = new Extent(-19705424, -14849545, 21700207, 21624981, new SpatialReference({ wkid: 102100 }));
        }

        map = new Map("map", {
            extent: initExtent
        });


        //let's add a basemap  
        var tiled = new Tiled(basemapURL);
        map.addLayer(tiled);

        where = 'Cito100_onderdeel=1';
        if (AREAisProvince == true) {
            where += ' AND Provincie=\'' + AREA + '\'';
        }
        map.on("load", getFeaturesToMapAndStorage(where));

        function getFeaturesToMapAndStorage(whereClause) {
            console.log(whereClause);
            //query the featureService  
            var query = new Query();
            query.returnGeometry = true;
            query.outFields = ["*"];
            query.outSpatialReference = new SpatialReference({ wkid: 102100 });
            query.where = whereClause;

            var queryTask = new QueryTask(contentFeatureURL);

            queryTask.on("plete", function (event) {
                //map.graphics.clear();  
                var featureGraphicsLayer = new GraphicsLayer();


                //@TODO: Can't we find a way to use the symbols from the featurservice directly, instead of the url's?  
                defaultSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10,
            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            new Color([255, 0, 0]), 1),
            new Color([0, 255, 0, 0.25]));

                var renderer = new UniqueValueRenderer(defaultSymbol, "Type");
                renderer.addValue("Plaats", new PictureMarkerSymbol("https://services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_nederland/FeatureServer/0/images/89e5f81878a69f9cc0525c841f98af54", 11, 11));
                renderer.addValue("Gebied", new PictureMarkerSymbol("https://services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_nederland/FeatureServer/0/images/165c76bd4465728a34f6d18df4a1ec03", 28, 28));
                renderer.addValue("Water", new PictureMarkerSymbol("https://services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_nederland/FeatureServer/0/images/f9c146a401f48c4f38202e83c2e4582a", 22, 22));
                renderer.addValue("Provincie", new PictureMarkerSymbol("https://services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_nederland/FeatureServer/0/images/7a5373d8f1dcd1ecc03cefbab687b97c", 38, 32));
                renderer.addValue("Land", new PictureMarkerSymbol("https://services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_europa/FeatureServer/0/images/7a5373d8f1dcd1ecc03cefbab687b97c", 38, 32));
                renderer.addValue("Werelddeel", new PictureMarkerSymbol("https://services.arcgis./nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Topografie_in_de_klas_wereld/FeatureServer/0/images/48f2256a49253388488d813d721c054b", 32, 38));

                var features = event.featureSet.features;
                var featureCount = features.length;
                for (var i = 0; i < featureCount; i++) {
                    var graphic = features[i];
                    featureGraphicsLayer.add(graphic);
                }
                featureGraphicsLayer.renderer = renderer;
                map.addLayer(featureGraphicsLayer);

                //@TODO: Update localstorage    

            }); //end on queryTask plete  

            queryTask.execute(query, queryComplete);

            function queryComplete() {
                console.log("fire function queryComplete");
                //@TODO: reset progressbar  
            }; //End function queryComplete  


        } //end function getFeaturesToMapAndStorage  

        //The two closing tags below are essential and close the plete DOJO part.    
    } //end function after require (AMD style)  
    ); //end require
Share Improve this question edited Sep 10, 2019 at 13:12 Federico Zancan 4,9044 gold badges48 silver badges62 bronze badges asked Jul 27, 2014 at 8:31 Dennis HuninkDennis Hunink 5832 gold badges7 silver badges21 bronze badges 4
  • 1 You didn't add the link to live demo. – Ilya Luzyanin Commented Jul 27, 2014 at 8:33
  • 2 or tell us what line is giving you the error :( – Ryan Kempt Commented Jul 27, 2014 at 8:33
  • @RyanKempt His error's most likely in some minified event code (hence g). See my answer below. – MicronXD Commented Jul 27, 2014 at 8:45
  • Just added the link, sorry for that! – Dennis Hunink Commented Jul 27, 2014 at 12:04
Add a ment  | 

1 Answer 1

Reset to default 3

Your issue is with: map.on("load", getFeaturesToMapAndStorage(where));

You're calling getFeaturesToMapAndStorage which returns undefined, and passing that in as the "load" handler. I'm guessing that the minified version of whatever it is that you're using uses g as a reference to your handler.

Since g is undefined, you're basically saying undefined(loadEvent).


EDIT: I'm guessing you meant to do something like this:

map.on("load", function(){
    getFeaturesToMapAndStorage(where);
});

EDIT 2: For clarity, I'll explain what was wrong in more detail. The difference between what you had and what I suggested above, is that you were calling getFeaturesToMapAndStorage on the line containing map.on("load", getFeaturesToMapAndStorage(where)); (as opposed to calling it after map's load event). In order to call your function getFeaturesToMapAndStorage with a predetermined parameter, you need to call it from a function (like the anonymous function above - function(){...}) that would then be passed in as the event handler.

Alternatively, due to how you have things scoped, you could reference your where inside of getFeaturesToMapAndStorage in place of having a whereClause parameter.

发布评论

评论列表(0)

  1. 暂无评论