Background: I am trying to pull in JSON data to populate a careers section. I've successfully requested and pulled JSON data in my first $.getJSON()
request and populated the titles. The next step is pulling the posting ID URLs and opening a second request to pull the Description and Apply URL.
Problem: The second request works because I can see the objects pulled into the console but for some reason my loop does not populated the section with the description or URL. I also don't receive any console errors making it a bit difficult to pinpoint the issue. Does it have anything to do with Asynchronous request? I am a first time tinkerer with AJAX, JSON and APIs.
Some documentation on API if needed: https://dev.smartrecruiters.com/customer-api/posting-api/
// Create variable to append postings to
var postingsContainer = document.querySelector('div.job-container');
// Creates postings JSON request
$.getJSON('https://api.smartrecruiters.com/v1/companies/SynchronyGroup/postings', function(postings) {
// Check to see if data is being pulled
console.log(postings);
showJobs(postings);
});
// Function that pulls json data and populates careers section
function showJobs(jsonObj) {
// Variable that holds job postings json data
var jobs = jsonObj['content']
// Loop to create open position elements
for (var i = 0; i < jobs.length; i++) {
// Creates Column for job postings
var jobPosting = document.createElement('div')
jobPosting.setAttribute('class', 'col-12 col-md-6 col-lg-4 my-5 job-posting');
// Creates Job Title
var jobH5 = document.createElement('h5');
jobH5.textContent = jobs[i].name;
jobPosting.appendChild(jobH5);
postingsContainer.appendChild(jobPosting);
// Store job post IDs in var
var jobId = jobs[i].ref;
// Creates post 2nd ID JSON request
$.getJSON(jobId, function(data) {
// Check to see if data is being pulled
console.log(data);
showDetails(data);
});
}
}
//Function for posting description and apply url
function showDetails(data) {
// Loop to pull company description and apply url, then append to job posting element
for (var j = 0; j < data.length; j++) {
console.log("I work");
// Creates Company Desc. and Apply Link
var jobDetail = document.createElement('p');
var jobApply = document.createElement('a');
jobDetail.textContent = data[j].sections.companyDescription;
jobApply.setAttribute('href', data[j].applyUrl);
jobApply.setAttribute('class', 'btn-primary');
jobPosting.appendChild(jobDetail);
jobPosting.appendChild(jobApply);
}
}