Why is my JavaScript loop not being entered?

154
February 05, 2020, at 11:50 AM

I have a JavaScript function that iterates through the data received from a Java method. I can see the data variable is being populated but the loop is not being entered;

JavaScript - in the web console the data variable is populated as data=[{"id":"1","name":"Megan","pay":100.0}] the following error is displayed in the web console;

jquery-3.3.1.js:3963 jQuery.Deferred exception: Cannot use 'in' operator to search for 'length' in [{"id":"1","name":"Megan","pay":100.0}] TypeError: Cannot use 'in' operator to search for 'length' in [{"id":"1","name":"Megan","pay":100.0}]
    at isArrayLike (http://localhost:8080/jquery-3.3.1.js:498:33)
    at Function.each (http://localhost:8080/jquery-3.3.1.js:359:8)
    at Object.<anonymous> (http://localhost:8080/csvTest.js:10:11)
    at mightThrow (http://localhost:8080/jquery-3.3.1.js:3665:29)
    at process (http://localhost:8080/jquery-3.3.1.js:3741:12) undefined
$(document).ready(function() {
$('#test').click(function() {
     $.ajax({
            url: "/test" 
     }).then(function(data) {
        $.each(data, function (i, item) {
          console.log(item.name);
        });
        });
  });
});

Method that returns data;

  @GetMapping("/test") 
      @ResponseBody
        public String getEmployeePercentageMatch(HttpServletRequest request, HttpServletResponse response,String roleName, String percentageMatch) throws IOException {
          percentageMatch="50";
          List<Employee> matchEmployees = new ArrayList<Employee>();
            Optional<Role> currentRole=roleRepository.findByroleNameIs("SWEM"); 
            Optional<List<RoleMapSkill>> skillsForRoles = roleMapSkillRepository.getSkillsForRole(currentRole);
            List<RoleMapSkill> skillsForRole = skillsForRoles.get();
            for(int i=0; i<skillsForRole.size(); i++) {
                Map<String, String> roleSkillMap = new HashMap<String, String>();
                roleSkillMap.put(skillsForRoles.get().get(i).getIdSkill().getIdSkill(), skillsForRoles.get().get(i).getRating());
                System.out.println(skillsForRoles.get().get(i).getIdSkill().getIdSkill()+" "+roleSkillMap.get(skillsForRoles.get().get(i).getIdSkill().getIdSkill()));
            }
            List<Employee> allEmployees = employeeRepository.findAll();
            for(int i=0; i<allEmployees.size(); i++) {
                int totalPercentage=0;
                Employee currentEmployee = allEmployees.get(i);
                Optional<List<EmployeeMapSkill>> skillEmployeeMap = employeeMapSkillRepository.getSkillsForEmployee(currentEmployee);
                for(int j=0; j<skillsForRole.size(); j++) {
                    if(skillEmployeeMap.isPresent()){
                        Map<String,String> employeeSkillMap = new HashMap<String, String>();
                        employeeSkillMap.put(skillEmployeeMap.get().get(j).getIdSkill().getIdSkill(), skillEmployeeMap.get().get(j).getRating());
                    }
                }
                for(int k=0; k<skillsForRole.size(); k++) {
                    if(skillEmployeeMap.isPresent()){
                    int individualPercentageMatch=(100/Integer.valueOf(skillsForRoles.get().get(k).getRating()))*Integer.valueOf(skillEmployeeMap.get().get(k).getRating());
                    totalPercentage= totalPercentage+individualPercentageMatch;
                    }
                }
                int totalPercentageMatch = totalPercentage/skillsForRole.size();
                System.out.println(totalPercentageMatch);
                if(totalPercentageMatch>=Integer.valueOf(percentageMatch)) {
                    matchEmployees.add(currentEmployee);
                }

            }
            String json = new Gson().toJson(matchEmployees);
            PrintWriter writer = response.getWriter();
            writer.write(json);
            return null;
        }
Answer 1

That is JQuery; I don't like that but(and with help of all)... dataType: 'json' is missed

On your code

    $('#test').click(function() {
     $.ajax({
            url: "/test", 
            dataType: 'json'
     }).then(function(data) {
        $.each(data, function (i, item) {
          console.log(item.name);
        });
        });
  });

But, I'm not really sure...

Rent Charter Buses Company
READ ALSO
newline char \n for &lt;textarea&gt; placeholder is ignored on Safari

newline char \n for <textarea> placeholder is ignored on Safari

I see that this question has been asked here already, but no answer was provided so farI'm trying to add a multi-line placeholder for a textarea using jQuery

202
Update knockout-bound input field from jQuery autocomplete

Update knockout-bound input field from jQuery autocomplete

I have two text inputs on my page: One for a shorthand code and one for the text description of that codeBoth inputs have a jQuery autocomplete applied

181
Why in my case is return not ending the function

Why in my case is return not ending the function

I have this code I'm using for sticky header some other things

177
How to limit multiple input fields to together exceed a certain number [closed]

How to limit multiple input fields to together exceed a certain number [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

189