FeatureLayer.applyEdits() always erroring on update when it actually worked

8878
12
Jump to solution
10-29-2013 10:33 AM
DaveHighness
Occasional Contributor II
I am am using the FeatureLayer.applyEdits() method to update the attributes of a feature layer. The method is working and the attributes are being updated correctly but the callback always errors. When I look at the network response from the method it looks like the REST returned a correct 'operation completed' JSON object. The FeatureLayer.applyEdits() method seems to be erroring in parsing the REST response to the callback.

var feat = {     "attributes": {         "OBJECTID": 1094,         "Well_ID": 565656,         "GWIC_ID": "efg789",         "Geomethod_ID": 5,         "Township": "1N",         "Range": "1E",         "Section_": "1",         "Quarter_Section": "sw ne",         "Physical_Address_WellName": "Baxter and Puckett",         "Subdivision": "Test point 07",         "City_ID": 4,         "TotalDepth_ft_": "87"     } };  Feat_Layer01.applyEdits(null,[feat],null,function(res){     console.log('worked: '+dojo.toJson(res));     /*     if (res.length > 0){         if (res[0].success){             Ext.MessageBox.alert('MESSAGE', 'Updated well objectid: '+updres[0].objectId);         }         else {             Ext.MessageBox.alert('MESSAGE', 'Well update unsuccessful');         }     }     else {         Ext.MessageBox.alert('MESSAGE', 'Well objectid not found');     }     */ },function(er){     console.log('errored: '+dojo.toJson(er));     //Ext.MessageBox.alert('ERROR', 'Update Error:<br />'); }); 


It always goes to the Errorback function.

REST Response as seen in the Network Tab of JS development console:
{     "addResults": [],     "updateResults": [         {             "objectId": 1094,             "success": true         }     ],     "deleteResults": [] }


Is this a bug in the ArcGIS Server Javascript API? I can probably live with it for now since the edit is actually working but I'd prefer if the callback worked correctly.

Thanks, Dave
0 Kudos
1 Solution

Accepted Solutions
DaveHighness
Occasional Contributor II
Ok, including the geometry in the update graphic has made the FeatureLayer.applyEdits(method) happy. It is now going to the completed callback function and reporting back the OBJECTID and success = true flag.

I think that this should be fixed in the JS API. If you just want to update the feature attributes you should not have to include the geometry in the update JSON. The REST is actually working correctly that way but the JS API is not for some reason.

Thanks, Dave

View solution in original post

0 Kudos
12 Replies
BenFousek
Occasional Contributor III
The return on applyEdits is three objects not one. An error is thrown because of an error in the callback function not because the operation failed.

layer.applyEdits([feature], null, null, function (add, update, del) {
 array.forEach(add, function (a) {
  console.log(a.status);
 });
}, function (error) {
 console.log(error);
});

layer.applyEdits(null, [feature], null, function (add, update, del) {
 array.forEach(update, function (u) {
  console.log(u.status);
 });
}, function (error) {
 console.log(error);
});

layer.applyEdits(null, null, [feature], function (add, update, del) {
 array.forEach(del, function (d) {
  console.log(d.status);
 });
}, function (error) {
 console.log(error);
});
0 Kudos
DaveHighness
Occasional Contributor II
Ben, thanks, I totally missed that. I do have an applyEdits Add that is working as it should with the same FeatureLayer and I couldn't figure out why the Update wasn't working.

I made this change but it is still unfortunately throwing a JS error. The error returned to er.message is "Cannot call method 'toJson' of undefined". Here is my updated JS code.

I don't have a 'toJson' call in here so I think that must be buried in the applyEdits() method?

        Feat_Layer01.applyEdits(null,[feat],null,function(add, upd, del){
            if (upd.length > 0){
                if (upd[0].success){
                    Ext.MessageBox.alert('MESSAGE', 'Updated well objectid: '+upd[0].objectId);
                }
                else {
                    Ext.MessageBox.alert('MESSAGE', 'Well update unsuccessful');
                }
            }
            else {
                Ext.MessageBox.alert('MESSAGE', 'Well objectid not found');
            }
        },function(er){
            Ext.MessageBox.alert('ERROR', 'Update Error:<br />'+er.message);
        });


I tried copying your code straight in there with the same effect.

The REST is still returning what appears to be a successful JSON:
{
    "addResults": [],
    "updateResults": [
        {
            "objectId": 1094,
            "success": true
        }
    ],
    "deleteResults": []
}


BTW I am using JS API 3.6 and ArcServer 10.1 sp1

Dave
0 Kudos
BenFousek
Occasional Contributor III
Does it throw an error when you do nothing in the callback function?
0 Kudos
DaveHighness
Occasional Contributor II
Yes, it still does, even if the function is empty.
0 Kudos
BenFousek
Occasional Contributor III
I wonder if the object you're creating is causing the problem. Normally I always use a Graphic because the API uses the graphic object to create the json for the request even when updating just attributes. Anytime you get a toJson or fromJson error it's almost always because an object or json is malformed somehow or is something other than the to/from function expected. Properly creating a Graphic seems like safest way to go and best practice.

var feature = new Graphic();

feature.setAttributes({
  OBJECTID: 1094,
  Well_ID: 565656,
  GWIC_ID: "efg789",
  Geomethod_ID: 5,
  Township: "1N",
  Range: "1E",
  Section_: "1",
  Quarter_Section: "sw ne",
  Physical_Address_WellName: "Baxter and Puckett",
  Subdivision: "Test point 07",
  City_ID: 4,
  TotalDepth_ft_: "87"
});
0 Kudos
DaveHighness
Occasional Contributor II
I tried that and got the same result.

I've been using Chrome to test this and I just tried it in FireFox and got a new error message in the error callback. Maybe this is a clue:

Apply Edits Error:
_196.geometry is undefined


Maybe I have to include the geometry object in the update graphic not just the attribute object. I only want to do an attribute update. I'll try that.

That still doesn't explain why the REST seems to return the correct JSON output:
{
    "addResults": [],
    "updateResults": [
        {
            "objectId": 2294,
            "success": true
        }
    ],
    "deleteResults": []
}


Dave
0 Kudos
DaveHighness
Occasional Contributor II
Ok, including the geometry in the update graphic has made the FeatureLayer.applyEdits(method) happy. It is now going to the completed callback function and reporting back the OBJECTID and success = true flag.

I think that this should be fixed in the JS API. If you just want to update the feature attributes you should not have to include the geometry in the update JSON. The REST is actually working correctly that way but the JS API is not for some reason.

Thanks, Dave
0 Kudos
BenFousek
Occasional Contributor III
I'm surprised that you have to include geometry. A feature can be a related table record, i.e. no geometry.

Also, you've already selected a feature from the feature layer or otherwise have a hold of it somehow. Why not just update the feature and use it instead of creating a new one? I have a dataset with static geometry/attributes, but several related tables that the user edits. The feature is the record whether it has geometry or not. The snippet below is for an inspector that updates related table records, but any input method could be used to directly update a feature's attributes.

this.inspector.on('attribute-change', lang.hitch(this, function (result) {
  var feature = result.feature;
  var atts = feature.attributes;
  for (var i in atts) {
    if (i === result.fieldName) {
      atts = result.fieldValue;
    }
  }
  this.applyEdits(null, [feature], null, function (a, u, d) {
    console.log(u[0]);
  }, function (error) {
    console.log(error);
  });
}));
0 Kudos
DaveHighness
Occasional Contributor II
Ben, I have two related attribute tables in addition to the well point features that will require add, update and delete. I'm just starting into that now. I'll know soon if that works.

One thing I'm trying to remember is if I delete a well point, does the relationship class delete the related records or do I have to handle that in my code? On to the next hurdle.

Thanks, Dave
0 Kudos