2017-07-11 53 views
1

背景:私はキャリアセクションを作成するためにJSONデータを取得しようとしています。最初の$.getJSON()リクエストでJSONデータをリクエストして取り込み、タイトルにデータを設定しました。次のステップでは、ポスティングID URLを取得し、2番目のリクエストを開いてDescription URLとApply URLを取得します。

問題: 2番目のリクエストは、オブジェクトがコンソールにプルされているのがわかりますが、何らかの理由でループにセクションまたはURLが設定されていないために機能します。私はまた、問題を特定するのが少し難しい、コンソールエラーを受け取っていません。非同期リクエストとは何か関係がありますか?私はAJAX、JSON、APIを初めて使う人です。

必要に応じてAPIの一部のマニュアル:この作業コードは、ちょうどこのラインvar jobs = jsonObj['content']を変更する必要が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); 

    } 
} 
+0

'showDetails'メソッドの中に' jobPosting.appendChild(...) 'があります。この変数はこのスコープには存在しません。 – Draco18s

+0

問題はこの行です。 'var jobs = jsonObj ['content']'。 'content'は配列ではないオブジェクトなので、' var jobs = jsonObj.content; 'に変更してください。 –

答えて

1

にラインを変更するあなたの主な問題は、 VARをjobPosting の値です。

この変数は、各ループの繰り返しでSHOWJOBS機能で作成していますが、showDetails関数に、この変数を渡すことはありません。

この変数は各繰り返しで作成されるため、正しい値を渡す必要があります(詳細については、js closureをご覧ください)。あなたのケースでは、$ .getJSON(jobId、function(data){)を使用するだけで、変数名を使用することはできませんので、最後の値、つまりループの最後にある値を渡します。コンテキストを閉じる必要があり

あなたが生命維持を使用することがありますが、この質問を参照することができ、このについての詳細情報については、このachiveするために:。。。What is the (function() { })() construct in JavaScript?

例:

// 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; 
 

 
     // 
 
     // 
 
     // IIFE 
 
     (function (jobPosting) { 
 
      // Creates post 2nd ID JSON request 
 
      $.getJSON(jobId, function (data) { 
 

 
       // Check to see if data is being pulled 
 
       //console.log(data); 
 

 
       showDetails(data, jobPosting); 
 

 
      }) 
 
     }(jobPosting)); 
 

 
    } 
 
} 
 
//Function for posting description and apply url 
 
function showDetails(data, jobPosting) { 
 

 
    // 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 = 'showDetails: ' + data.company.name; 
 
    jobApply.setAttribute('href', data.applyUrl); 
 
    jobApply.setAttribute('class', 'btn-primary'); 
 

 
    jobPosting.appendChild(jobDetail); 
 
    jobPosting.appendChild(jobApply); 
 

 
    //} 
 
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 

 

 
<div class="job-container"></div>

+0

IIFEを使用するときにfor()が不要なので、2番目のループをコメントしたことがありますか? – WeebleWobb

+0

@WeebleWobbいいえ、申し訳ありませんが、変数が普通のオブジェクトであり、配列であるかのように循環することができないため、2番目の(内側の)getJsonのループをコメントしました。 **(キー入力データ)**などのオブジェクトに対しては、別のループ戦略を使用できます。ありがとう – gaetanoM

+0

よろしくお願いします。追加のIIFEスレッドを共有していただきありがとうございます。 lilは把握するが、動作するように見える。私は何かをしなければならないことをしていますが、私はより快適になるように引き合いを変えずに、正しい方向に歩み続けています。 – WeebleWobb

0

チェックアウト。 var jobs = jsonObj.content;

// 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); 
 

 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div class="job-container"></div>

関連する問題