Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,583 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) } };