promise all issue with applyEdits

1043
1
04-11-2014 08:20 AM
AaronHeyman
New Contributor III
I am trying to properly handle the errors generated by this code and am missing something fundamental.  I have 4 records to add.  I queue them up async and wait for 4 promises to fulfill before continuing.  But one of them fails.  How do I determine which of the 4 failed?

Thanks in advance.

var namesObj =  [    // sample namesObj it is loaded from a file.
                   { siteID:1,
      study_no:1 },
     { siteID:2,
      study_no:2 },
     { siteID:3,
      study_no:3 },
     { siteID:4,
      study_no:4 },
    ];
var counter = 0;
var deferredArray = [];
for (var i=0; i<namesObj.length; i++) {
myNewAttributeArray = {
   "SiteID" : namesObj.SiteID,
   "Study_NO" : namesObj.study_no
};
var myG = new esri.Graphic(null, null, myNewAttributeArray, null);
// featureLayers[14] is created much earlier in the code.
deferredArray[counter] = gl.featureLayers[14].applyEdits([myG],null,null,function(result) {
  // add success
}, function(error) {
  // add fail, "error" message contains nothing about what failed
  // How can I pass something into this function so that I know which failed so I can try
  //  again to add the record, or output a useful message to the user?
});
counter++;
}
all(deferredArray).then( function() {
// this fires if all DB adds succeed
}, function() {
// this never fires
});

This is the "error" message.
error.message is "Unable to complete operation."
error.details[0] is "Unable to perform applyEdits operation."
0 Kudos
1 Reply
JianHuang
Occasional Contributor III
The error should be self explaining which request has failed. If not, we need to improve it.
In order to work it around, you have to add extra indicator to the result when deferred gets resolved. For example:

var df = fl.applyEdits(edits);
df.then(lang.hitch(null, function(id, response){
  //now you have id for this specific request
}, id));
//have a list of deferreds
all(deferreds).then(function(results){
  //loop through the results, and you will find each result should have an id associated to.
});
0 Kudos