2017-08-05 63 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如何被鏈接的問題與當前的問題? – guest271314

+1

@ZachFrotten請參閱https://stackoverflow.com/help/someone-answers – guest271314

回答

3

這不是你如何使用fetch API。使用response.json()這樣的(記錄一個錯誤,因爲我不知道apiIdapiKey):

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

相關問題