Hi everyone Im having trouble getting the last element of the unique values
const layer = FeatureLayer({
url: "",
});
const layer2 = FeatureLayer({
url: "",
refreshInterval:0.5
});
var id;
document.getElementById("animal").addEventListener("change", () => {
var arr=[];
const query= layer.createQuery();
query.where = "Animal= '" + document.getElementById("animal").value +"'";
query.returnGeometry = false;
query.returnDistinctValues = true;
query.outFields=["ID", "Animal"];
layer.queryFeatures(query).then(function(response){
const values = response.features;
if (values.length >0) {
for (var i =0; i < values.length; i++) {
arr[i] = values[i].attributes['ID'];
}
id= parseInt(arr.at(arr.length-1))+1
}
else {
id= 1;
}
});
});
document.getElementById("createdata").onclick = () => {
const long = document.getElementById("LONG").value;
const lat = document.getElementById("LAT").value;
const point = {
type: "point",
longitude: long,
latitude: lat,
};
const simpleMarkerSymbol = {
type: "simple-marker",
color: [226, 119, 40],
outline: {
color: [12, 137, 148],
width: 0.5,
},
};
const pointGraphic = new Graphic({
geometry: point,
symbol: simpleMarkerSymbol,
attributes: {
Name: document.getElementById("name").value,
Animal: document.getElementById("animal").value,
ID: id
},
});
layer2.applyEdits({ addFeatures: [pointGraphic] });
document.getElementById("animal").value = "";
document.getElementById("name").value = "";
};
<div class="esri-widget">
<input id="city" size="15" />
</div>
So I'm trying to generate new ids and add them to the feature layer. In order to do so I want to see if the animal inputted by the user exists in the data, if it does it should get the unique values, extract the last unique id (max id) and add 1 to it. But, if the animal doesn't exist in the data, it should assign id =1.
The id should then be assigned as an attribute to the graphic, which is then added to the layer2 by doing: layer2.applyEdits({ addFeatures: [pointGraphic] }); (* layer2 is a feature layer view of layer*)
The problem is it works but sometimes it duplicates the last stored id and adds it again. So, for instance, if the current max id is 72, and I add keep adding data, at some random point, the id repeats itself when i add new data. There are also times when I will randomly get 1 even if I have the animal is in the data.