Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript A Real World Example

Mary Paul
Mary Paul
13,792 Points

Getting "object, object" for all my employees in the widget (no reported errors)

So, I got the widget.js to display, but instead of employees it just says "object, object". Not quite what I'm going for. Thoughts?

snapshot of my workspace here: https://w.trhou.se/5jgqyc94mb

widget.js

function getJSON(url) {
  return new Promise(function(resolve, reject){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.onreadystatechange = handleResponse;
    xhr.onerror = function(error){ reject(error); };
    xhr.send();

    function handleResponse() {
      if(xhr.readyState === 4) {
        if(xhr.status === 200){
          var employees = JSON.parse(xhr.responseText);
          resolve(employees)
        } else {
          reject(this.statusText);
        }
      }
    };

  });
}

var ajaxPromise = getJSON('../data/employees.json');



var xhr = new XMLHttpRequest();
xhr.open('GET', '../data/employees.json');
xhr.onreadystatechange = handleResponse;
xhr.send();

function handleResponse() {
  if(xhr.readyState === 4 && xhr.status === 200) {
    var employees = JSON.parse(xhr.responseText);
    addEmployeesToPage(employees)
  }
};

function generateListItems(employees)  {
    var statusHTML = '';
    for (var i=0; i<employees.length; i += 1) {
        if (employees[i].inoffice === true) {
            statusHTML += '<li class="in">';
        } else {
            statusHTML += '<li class="out">';
        }
        statusHTML += employees[i].name;
        statusHTML += '</li>';
    }

    return statusHTML;
}

function generateUnorderedList(listItems) {
    return '<ul class="bulleted">' + listItems +  '</ul>';
}

function addEmployeesToPage(unorderedList) {
    document.getElementById('employeeList').innerHTML = unorderedList;
}



ajaxPromise.then(generateListItems)
  .then(generateUnorderedList)
  .then(addEmployeesToPage)
  .catch(function(e){
     console.log(e);
  });
Steven Parker
Steven Parker
229,645 Points

A great way to share code with multiple dependencies is to make a snapshot of your workspace and post the link to it here.

1 Answer

Abel Castillo
Abel Castillo
19,094 Points

Remove the repeated code, it already exist inside getJSON : var xhr = new XMLHttpRequest(); xhr.open('GET', '../data/employees.json'); xhr.onreadystatechange = handleResponse; xhr.send();

function handleResponse() { if(xhr.readyState === 4 && xhr.status === 200) { var employees = JSON.parse(xhr.responseText); addEmployeesToPage(employees) } };