1

I have a beginner question. I am using an ajax call to get elements of a sharepoint list choice(checkboxes) column called "Orbit" to create a google pie chart:

data.d.results.forEach(function(row) {
    // each row
    row.Orbit.results.forEach(function(choiceOrbit) {
        // each choice of Orbit of single row
        if (countOrbit.hasOwnProperty(choiceOrbit)) {
            countOrbit[choiceOrbit] += 1;
        } else {
            countOrbit[choiceOrbit] = 1;
        }
    });

But if the item EDIT: (if the row is empty) i get an error:

Uncaught TypeError: Cannot read property 'results' of null
at Statistics.aspx:658
at Array.forEach (<anonymous>)
at Object.success (Statistics.aspx:632)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest.<anonymous> (jquery.min.js:4)

How can i add an if(), so that an empty item will be neglected in the counting? Something like:

if(row.Wavebands.results.forEach!=null) 

doesnt work

2 Answers 2

1

I think the correct answer is:

data.d.results.forEach(function(row) {
  // each row
  if (row.hasOwnProperty('Orbit') && row.Orbit != null) {
    row.Orbit.results.forEach(function(choiceOrbit) {
      // each choice of Orbit of single row
      if (countOrbit.hasOwnProperty(choiceOrbit)) {
          countOrbit[choiceOrbit] += 1;
      } else {
          countOrbit[choiceOrbit] = 1;
      }
    });
  }
});
0
3

Try to check the length of the data.result

 if(data.d.results.length > 0)
  {
    // Add your code

  }
5
  • 1
    doesnt work.... not the whole column is empty, only one item ( so one of the forEach)
    – Fabian M
    Commented Oct 8, 2018 at 13:19
  • 1
    in the column of the sharepoint list, some rows are empty. this is causing the problem
    – Fabian M
    Commented Oct 8, 2018 at 13:33
  • Okay try to check if(row!=null) before going to foreach Commented Oct 8, 2018 at 14:31
  • 1
    its not working. Both before and after row.Orbit.results.forEach(function(choiceOrbit)
    – Fabian M
    Commented Oct 8, 2018 at 14:38
  • This will also not work if the list is initially empty, and you're trying to figure out if someone has written anything to it before you write an item. It will complain that the list doesn't exist (even though it does).
    – vapcguy
    Commented Aug 13, 2019 at 19:02

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.