0

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;
    }
}
1

You are accessing all items in the list with your code. So you basically get a collection back and need to iterate over the items. That's usually killing your performance ;)

Also it helps if you give us the URL you want to hit with a regexp in order to extract the ID. Try running the RegExp against those URLs in a test page to see what happens (I hate debugging RegExp - Its write once - read never code)

If you know the ID of the item you need to retrieve, you should use a slightly different syntax (I am not using quotes):

/_api/web/lists/getbytitle('testlist')/items(1)

This will only return one item at a time.

You can access those API calls (for gets at least) quite easy in the browser if you want to verify them 1st. In IE you should disable the "Feed reading view" under the content Options of the internet settings, so it wont mess up the XML.

  • thank you very much for the answer, I had previously tried this solution, but reading what you wrote I understood where I was wrong, basically the variable itemId was a string and not a number, I solved using parseInt and following your recommendations! – Federico Iannarelli Oct 17 '18 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.