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

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;
      }
    });
  }
});
3

Try to check the length of the data.result

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

  }
  • 1
    doesnt work.... not the whole column is empty, only one item ( so one of the forEach) – Fabian M Oct 8 '18 at 13:19
  • 1
    in the column of the sharepoint list, some rows are empty. this is causing the problem – Fabian M Oct 8 '18 at 13:33
  • Okay try to check if(row!=null) before going to foreach – Mohamed El-Qassas MVP Oct 8 '18 at 14:31
  • 1
    its not working. Both before and after row.Orbit.results.forEach(function(choiceOrbit) – Fabian M Oct 8 '18 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 Aug 13 at 19:02

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.