The strings that I want to printed in the console earlier than the json data. I am now working with the food data central API
const fetch = require("node-fetch");
const params = {
api_key: '<api_key>',
query: 'chicken breast raw',
dataType: ["Survey (FNDDS)"],
pagesize: 5,
}
const api_url = `https://api.nal.usda.gov/fdc/v1/foods/search?api_key=${encodeURIComponent(params.api_key)}&query=${encodeURIComponent(params.query)}&dataType=${encodeURIComponent(params.dataType)}&pageSize=${encodeURIComponent(params.pagesize)}`
function getData(){
return fetch(api_url).then(response => response.json())
}
console.log("Protein:")
getData().then(data=> console.log(data.foods[0].foodNutrients[0].value))
console.log("Fats:")
getData().then(data=> console.log(data.foods[0].foodNutrients[1].value))
console.log("Carbs:")
getData().then(data=> console.log(data.foods[0].foodNutrients[2].value))
Output:
Protein: Fats: Carbs: 8.8 0 26.2
I just want to print first the string "protein" after the protein value from the json and so on