Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


OpenLayers / GeoServer + PostGIS


I have a map with multiple WMS layers and of these I need to refresh a few after inserting a polygon in one of the layers.


Given below are three options of the AJAX success callback function on successful insert of polygon geometry in the PostGIS backend and the corresponding errors/observations.

Option 1:

if(data == "SUCCESS")

// Throws error: Uncaught TypeError: Cannot call method 'redraw' of undefined

Option 2:

var tempLayer;
if(data == "SUCCESS")
   console.log("Prev cmd is successful");
   tempLayer = map.getLayersByName('bldg_logical')[0]

// Outputs: 'Prev cmd is successful'
// tempLayer gives proper layer object: OpenLayers.Layer.WMS.OpenLayers.Class.initialize {metadata: Object, options: Object, singleTile: true, ratio: 1, isBaseLayer: false…}
// But still throws error: Uncaught TypeError: Cannot call method 'redraw' of undefined

Option 3:

if(data == "SUCCESS")

// Works fine (but I need both layers to be refreshed)


Could anyone let me know why the error occurs?
Is my usage wrong?
Does it have to do anything with the order in which the layers have been added to the map?

share|improve this question
Are you doing anything besides this in the ajax call? Also I would reference the layers directly not with a method lookup. – Frank Phillips Jun 15 '14 at 6:19

It looks to me like your layers named 'bldg_logical' don't exist. 'Undefined' means you have an uninitialized property. This implies that the getLayersByName method isn't finding any layers named 'bldg_logical'.

share|improve this answer
If you check the Option 2 section, the tempLayer variable gets the Layer object ie., getLayersByName works and the first element is present in the result array which implies the bldg_logical layer is present. So why should it throw an undefined error in that case? – Siva Jul 23 '13 at 10:04
Are you sure? When map.getLayersByName('bldg_logical') doesn't find any match, it returns empty array []. So you can successfully do tempLayer = [][0] and tempLayer will be undefined. You should add console.log(tempLayer) before tempLayer.redraw(true); – user1702401 Feb 5 at 7:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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