2017-08-05 16 views
0

このファイルを実行すると何も得られません。代わりにconsole.log(getInfo());を実行した場合、私はちょうどPromise <pending>を得ます。助けてください。私のapiリクエストは何も返されません。エラーメッセージもありません

function getInfo(){ 
    var url = `https://api.nutritionix.com/v1_1/search/cheddar%20cheese?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat&appId=${apiId}&appKey=${apiKey}`; 
    return(
    fetch(url) 
    .then(data=>{ 

     return JSON.parse(data); 
    }) 
    ); 
} 

getInfo().then(result =>{ 
    console.log(result); 
+1

現在の質問に関連する質問をリンクされている方法@JF:この(私はapiIdapiKeyを知らないため、エラーを記録します)のようなresponse.json()を使用しますか? – guest271314

+1

@ZachFrotten https://stackoverflow.com/help/someone-answersを参照してください。 – guest271314

答えて

3

これはfetch APIの使用方法ではありません。

function getInfo(){ 
 
    var apiId = 1; 
 
    var apiKey = 1; 
 
    var url = `https://api.nutritionix.com/v1_1/search/cheddar%20cheese?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat&appId=${apiId}&appKey=${apiKey}`; 
 
    return fetch(url).then(response => response.json()); 
 
} 
 

 
getInfo().then(data => console.log(data));

関連する問題