Select to view content in your preferred language

graphic.attributes is not defined

1589
2
08-19-2013 12:52 PM
Chang-HengYang
Regular Contributor
Hi Everyone,

I tried to modify the example listed below to zoom in the graphic. However, the Error Console keeps to tell me that is a error "TypeError: graphic.attributes is undefined" in the test with the red color. Please let me know if you can help point out the question.

Many thanks,

Hank

The example (StateZoomin): http://serverapps.esri.com/javascript_examples/StateZoom.html

My application: http://granite.nmt.edu/~hyang/test1.html
(Please use this link to see the source code)


function zoomRow(id){
selectionLayer.clear();
dojo.some(map.graphics.graphics,function(graphic){
if (graphic.attributes.OBJECTID.toString() === id) {
var selectedState = new esri.Graphic(graphic.geometry).setAttributes(
graphic.attributes);
selectionLayer.add(selectedState);
//Zoom to the extent of the parcel - expand it a bit so we aren't zoomed in too close.
var stateExtent = selectedState.geometry.getExtent();
map.setExtent(stateExtent);
return true;
}
});
}
0 Kudos
2 Replies
JohnGravois
Deactivated User
its probably not going to solve the specific problem you're inquiring about, but it seems problematic to me that you use the legacy dojo.require() to load most of your modules, then AMD load a few more at line 253.  is there a particular reason why you're doing this?

it seems like it would be a lot more straightforward to set these properties at the beginning of your init() function at line 150 instead
esri.config.defaults.io.proxyUrl = "/proxy.ashx";
esri.config.defaults.io.alwaysUseProxy = false;
...
esri.config.defaults.map.zoomRate


regarding your actual question...
its kind hard to tell whats going on since the featureLayer which triggers the workflow isn't actually shared publicly.  perhaps you can try substituting a public service and perhaps even simplifying the code?
0 Kudos
derekswingley1
Deactivated User
Instead of assuming that an object (a graphic in this case) will always have a particular property, it's safer to check for its existence first.

With your code, instead of trying to access graphic.attributes.OBJECTID right away, do this:
function zoomRow(id) {
  selectionLayer.clear();
  dojo.some(map.graphics.graphics, function (graphic) {
    if (graphic.hasOwnProperty("attributes") && 
        graphic.attributes.hasOwnProperty("OBJECTID") &&
        graphic.attributes.OBJECTID.toString() === id) {
      var selectedState = new esri.Graphic(graphic.geometry)
        .setAttributes(
          graphic.attributes);
      selectionLayer.add(selectedState);
      //Zoom to the extent of the parcel - expand it a bit so we aren't zoomed in too close.
      var stateExtent = selectedState.geometry.getExtent();
      map.setExtent(stateExtent);
      return true;
    }
  });
}
0 Kudos