applyEdits of a related table record fails occasionally

2278
10
03-28-2017 12:23 PM
KarenRobine
Occasional Contributor II

I am attempting to applyEdits of a record for a related table that is being updated.  My steps are to first set the attributes using record.setAttributes(attributes); I've included all the attributes (including the ObjectID). I then run applyEdits on the proper feature layer. Most of the time it works. Occasionally, however, I receive the following error: "Cannot read property 'attributes' of undefined".  I've noticed that even though I receive this error message, the update still works.  And as I mentioned above, this doesn't occur every time.  Thoughts on why I'm getting this, and how to adjust my code so that this never occurs?

0 Kudos
10 Replies
SarojThapa1
Occasional Contributor III

Karen, 

Is the OBJECTID in READONLY mode? It would be easier for us to tell what is wrong if you can post your codes.

Check this sample if you haven't already done so.

Using the attribute inspector | ArcGIS API for JavaScript 3.20  

0 Kudos
KarenRobine
Occasional Contributor II

No, it shouldn't be in READONLY mode, but what is the mechanism to place the OBJECTID in READONLY mode?  I have been deleting, adding, and updating to the table without problems so I'm assuming that's not the problem.

I'm skipping a little code, but here is concept. Please keep in mind that most of the time it works. 

var attributes = recrd.attributes;

//do some changes

recrd.setAttributes(attributes);

//now apply edits, but I use a deferred since I am handling adds, updates and deletes (I do deletes a bit differently which is why its null). I also don't do adds and updates at the same time.  Typically, I'd either only do an update, or combine adds and deletes .

var def = new Deferred();

fLayer.applyEdits(newAttributes, updateAttributes, null).then(function(adds, updates, deletes) {

if (requestType === "add" && adds && adds.length > 0) {

//just get the first one

def.resolve({adds: adds[0]});

} else if (requestType === "update" && updates && updates.length > 0) {

//just get the first one

def.resolve({updates: updates[0]});

}

}, function(error) {

if (requestType === "update" && error && error.message && error.message.indexOf("Cannot read property 'attributes' of undefined") > -1) {

console.log("SaveEditsHandler.js: _prepareAllRequests() we got the Cannot read property 'attributes' of undefined error but we pass it back");

def.resolve({updates: {success: true}}); //TODO: I think this needs the objectId

} else {

def.reject();

}

});

return def.promise;

0 Kudos
SarojThapa1
Occasional Contributor III

I strongly believe you should not be editing objectid. The OBJECTID is a system primary key index of guaranteed unique values for that table that is read only, used by geoprocessing tools, scrolling and other system functions. 

There are two ways you can make OBJECTID in READONLY mode:

1. Do not display objectid in the attribute table 

2. 

var layerInfos = [{
    'featureLayer': featureLayer,
    'showAttachments': false,
    'isEditable': true,
    'showDeleteButton': false,
    'fieldInfos': [
        {'fieldName': 'Name', 'isEditable': false, 'label': '<b> Site Name:</b>'},

I don't see anything wrong with your code. If you have time, create a new database table with the same fields and data and don't mess up with objectid at all this time.

Good luck! 

0 Kudos
KarenRobine
Occasional Contributor II

interesting...


So regarding Item #1: Do not display objected in the attribute table,  would the change be made on the Web Map?  If I show the tables for all my related tables in ArcGIS Online, I do notice that the OBJECTID field does not appear. When I click "Options"  and show/Hide columns, the OBJECTID field is always turned off.  Therefore, I think I'm good, right? 

Regarding Item #2:  I'm not following where you're going with that?

So, in my setAttributes() method, should I pull out OBJECTID field?  Should I ONLY set attributes on "changed" attributes?


Out of curiosity, do you know if  this documented anywhere?

Thanks so much for your help.

0 Kudos
KarenRobine
Occasional Contributor II

I tried by removing the OBJECTID field, as well as any other fields I'm not changing.  I get the following error (see below):

I also checked and my Feature Service has OBJECTID set as follows:

OBJECTID (type: esriFieldTypeOID, alias: OBJECTID, SQL Type: sqlTypeOther, length: 0, nullable: false, editable: false)

        1. Error: Must declare the scalar variable "@OBJECTID". at Object.constructor (https://js.arcgis.com/3.19/init.js:2035:385) at new <anonymous> (https://js.arcgis.com/3.19/init.js:160:49) at Object._editHandler (https://js.arcgis.com/3.19/init.js:2006:423) at Object.load (https://js.arcgis.com/3.19/init.js:1965:346) at https://js.arcgis.com/3.19/init.js:993:143 at c (https://js.arcgis.com/3.19/init.js:103:393) at d (https://js.arcgis.com/3.19/init.js:103:182) at b.Deferred.resolve.callback (https://js.arcgis.com/3.19/init.js:105:10) at c (https://js.arcgis.com/3.19/init.js:104:96) at d (https://js.arcgis.com/3.19/init.js:103:182)
        2. objectId:-1
        3. success:false
        4. __proto__:Object
      1. __proto__:Object
    1. length:1
    2. __proto__:Array[0]
0 Kudos
SarojThapa1
Occasional Contributor III

Karen, 

Did you try set attributes only on "changed" attributes?

0 Kudos
SarojThapa1
Occasional Contributor III
0 Kudos
KarenRobine
Occasional Contributor II

Thanks Saroj. I'll try that. But based on my previous comment, it seems like I have to include the OBJECTID as well (otherwise, I get that error I noted in my previous post). 

0 Kudos
thejuskambi
Occasional Contributor III

Hello Karen,

Can you share the part of the code where you create the variable "newAttributes" and "updateAttributes". The error message looks like it is not getting a graphic object passed to the applyEdits method. Can you check if a null or undefined is passed as an array item.

Hope this was helpful.