I got a problem with the item id, at the moment i could create a new item without problems but i can't read it since i delete 2 items, now when i try to retrieve the item properties i got this error : Uncaught TypeError: Cannot read property 'Title' of undefined
I'm using the following code:
var promise = $.ajax({
url: "/siti/ddc/UTPG/areaPrivata/_api/web/lists/getbytitle('Prova Scheda')/items?$select=AllegatoId",
type: "GET",
headers: {
"ACCEPT": "application/json;odata=verbose"
},
success: function(data){
var itemId = getParameterByName("ID");
Attachments = data.d.results[itemId].AllegatoId.results;
},
error: function(error){
console.log(JSON.stringify(error));
}
});
promise.then(function(){
$.ajax({
url: "/siti/ddc/UTPG/areaPrivata/_api/web/lists/getbytitle('Allegati')/items?$select=Title,EncodedAbsUrl",
type: "GET",
headers: {
"ACCEPT": "application/json;odata=verbose"
},
success: function(data){
try {
console.log(Attachments);
for (var i=0; i < Attachments.length; i++){
var docId = Attachments[i];
// console.log(data.d.results[5].EncodedAbsUrl);
console.log(data.d.results[docId].EncodedAbsUrl);
$("#listaAllegati").append("<span class='fstChoiceItem'><a href='"+data.d.results[docId].EncodedAbsUrl+"'>"+data.d.results[docId].Title+"</a></span>");
}
}catch(err){
// console.log(JSON.stringify(err));
// console.log(err);
return false;
}
}
});
});
I can't understand why it doesn't work
To get the id, the function that i used is as follows :
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null) { return ""; } else {return decodeURIComponent(results[1].replace(/\+/g, " ")); }
}
EDIT
I just tried to put the item id in the url but it doesn't work :
/siti/ddc/UTPG/areaPrivata/_api/web/lists/getbytitle('Prova Scheda')/items("+itemId+")
EDIT 2
I managed to find a temporary solution, if there are better solutions I would be grateful
My solution is as follows :
for (var i = 0; i < $(data.d.results).length; i ++){
if (data.d.results[i].Id === itemId){
itemId = i;
break;
}
}