How to update hosted feature layer on ArcGIS Online with JavaScript?

214
4
01-10-2019 11:46 AM
FrancisPolignano1
New Contributor II

I'm trying to update values on a feature layer that is hosted on ArcGIS online. When I run the below code, nothing is updated but I do notice that the date modified changes to reflect the last time I run the code. Any ideas? I feel like it might have to do with settings/permissions on the hosted layer? I made sure editing was allowed and tried making it publicly shared as well. Any help is appreciated. Thanks

const layer = new FeatureLayer({
});

const query = new Query();
query.where = "ISO = 'VA'";
query.outSpatialReference = {
wkid: 102100
};
query.returnGeometry = true;
query.outFields = ["haveData", "ISO",];

layer.queryFeatures(query).then(function (results) {

console.log('before update');
console.log(results.features[0].attributes.haveData);

// update the appropriate value
results.features[0].attributes.haveData = 22.0;
console.log('after update'); // this checks out, displays what I want

console.log(results.features[0].attributes.haveData);
console.log(results.features[0]) // this also displays the new value that I want

let edits = {
updateFeatures: [results.features[0]]
};

layer.applyEdits(edits).then(function (editsResult) {

})
.catch(function (error) {
console.error("[ applyEdits ] FAILURE: ", error.code, error.name,
error.message);
});
});
Tags (2)
0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Francis,

  1. A FeatureLayer has to point to a specific layer in a service your url does not have the layer id in the url. that is an issue.
  2. Values of non nullable fields must be provided when updating features. I am not sure if this is an issue in your scenario or not.
0 Kudos
FrancisPolignano1
New Contributor II

Thanks Robert, 

I've sort of obscured the URL just for privacy reasons. I am able to query, select and view all the data I need in the correct feature layer so I'm thinking that is not an issue unless I'm misunderstanding what you're saying a little bit. I'm not sure how to get a different link for my FeatureLayer I'm using the one listed on ArcGIS online. 

In my scenario there are no null values and I'm only trying to change an already populated value to something else. 

Thanks again for the help

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Francis,

   Is there more than one layer in this feature service?

0 Kudos
FrancisPolignano1
New Contributor II

Robert, 

I figured it out. I had to change 

query.outFields = ["haveData", "ISO",];

to

query.outFields = ["*",];

I guess that makes sense, as what I was feeding the applyEdits() didn't exactly match what I was trying to update.