0

I am using this code to retrieve data from my sharepoint list. Regardless of my Null check I still get the error saying unable to get property or null reference.

 $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + requestUri,
        type: "GET",
        async:false,
    headers: {
            "accept": "application/json;odata=verbose",
    },
            success: function (data) {
                $.each(data.d.results, function (i, item){
                    var stat = item.MijnStatus;
                    var ltiov = item.LTIOV;
                    var reply = item.Reply;
                    var periode = item.Jaar;
            if (periode == myYear || stat == 'Completed') {
                console.log(ltiov);
                console.log(reply);
                                     if (reply != null){
                                        xValues.push(item[i].Reply);
                                      }
                                     if (ltiov != null){
                                        xValues.push(item[i].LTIOV);
                                      }
                var randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
                        barColors.push(randomColor);

            }
               // fnGenerateChart(xValues, yValues);
                })

            },
            error: function () {
                alert("Something went wrong");
            }                     
        });    
5
  • At which line are you getting this error? Instead of using item[i].LTIOV, try using item.LTIOV. Let me know if this works? Commented Mar 8, 2022 at 8:24
  • Ganesh indeed this works! The only thing not working is this line: if (periode == myYear || stat == 'Completed') . I still get all items also the ones not Completed.
    – Marco
    Commented Mar 8, 2022 at 8:30
  • You are using **OR**(||) operator in condition. Are you sure you want that? You are getting items that are not completed because those items satisfies first condition (periode == myYear). If you want both conditions to satisfy, use if (periode == myYear && stat == 'Completed'). Commented Mar 8, 2022 at 8:52
  • 1
    That makes sense, thanks a lot!
    – Marco
    Commented Mar 8, 2022 at 8:57
  • Great, glad it worked for you. Commented Mar 8, 2022 at 9:14

1 Answer 1

1

To solve this error, use item.LTIOV instead of using item[i].LTIOV as you are getting complete list item object in item variable inside for each loop.

Updated from comments:

To get only "Completed" items, use if condition like below:

if (periode == myYear && stat == 'Completed') {  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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