简体   繁体   中英

My api request is returning nothing, no error message either

when i run this file, i get nothing. If i run instead console.log(getInfo()); at the end i just get Promise <pending> . Please help.

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

This is not how you use the fetch API . Use response.json() like this (logs an error because I don't know the apiId and apiKey ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM