Javascript Error features[0].geometry.spatialReference.wkid

2893
2
09-11-2014 09:50 AM
BrianO_keefe
Occasional Contributor III

So we've got an odd and anomalous situation going on.

 

We have two servers. One is TEST and one is DEV.

 

On the TEST server, the map works fine. It DID throw an error, but I added some TRY / CATCH to the mix and it solved the issue.

The EXACT same code was copied over to DEV (I know... going backwards, whole nother story) and suddenly it doesn't work.

 

The code in question that is throwing the error is:

 

var wkid = features[0].geometry.spatialReference.wkid;

 

Now, after this code there is a block of code with IF THEN / ELSE blocks that runs this:

 

function(feature)

{

     point = esri.geometry.geographicToWebMercator(feature.geometry);

     ( or point = feature.geometry; based on the WKID returned )

     point.attributes = feature.attributes;

     this._features.push(point);

}

 

The interesting thing is that on the TEST where this works, I added in some console.log calls with objToString (custom function) to print me out some human readable details and so I see all of the objects being sent to the screen. Now in DEV this SAME function, pulling same data, drops a javascript error alert window.

 

[object Error]

 

When you click the OK button it pauses and then proceeds to correctly fill the screen. So the code AFTER the WKID part runs like it's supposed to on TEST, but on DEV it throws that error... and then runs like it's supposed to. On TEST I was able to stop this kind of tom foolery by placing a TRY / CATCH and all goes as planned. But on DEV I still get the error.

 

Oh, on DEV (where the error is being thrown) I should note that I added a console.log call to objToString(features[0].geometry) and geometry is null. Yet after you click OK on the error window the dots fill the map?!?!?!

 

Confused.

0 Kudos
2 Replies
ArtemisFili
Esri Contributor

Not sure why the features display on the map while the geometry is null. It could be that only some of the features have a null geometry and they are causing the error. An idea would be to check the same features directly in the ArcGIS Server REST endpoint to see what is going on with the geometry.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Brain,

It really sounds like you are trying to work with this features array before it is ready.

Whether it works on one machine and not the other is not a definitive test to whether you are trying to loop through the features array to early or not. One machine might be quicker to get the results than the other or any number of reasons.

   I would check your objects with something like this:

if(features && features[0] && features[0].geometry && features[0].geometry.spatialReference && features[0].geometry.spatialReference.wkid){

//do something

} else {

//features is null or the first feature in the array is null or it's geometry is null, etc, etc

}