skip to Main Content

Context:
I have a script that utilizes the Nutritionix API to retrieve nutritional values for various food items. While the API returns valid values for some nutrients, it returns ‘NaN’ for others. I suspect this issue could be due to my usage of the free version of the API or a potential error in my code, possibly related to the item IDs. (An example of what I mean by item IDs includes nf_biotin and nf_iron, both of which should be returning values and working correctly.)

Script:


let table = base.getTable('Raw Meals 2')
let {records} = await table.selectRecordsAsync()
console.log(records)

for (let record of records){
 const url = 'https://trackapi.nutritionix.com/v2/natural/nutrients';
 const options = {
  method: 'POST',
  headers: {
      'Content-type' : 'application/json',
      'x-app-key':'--',
      'x-app-id': '--'
      },
   body: JSON.stringify({
     'query':`${record.name}`
   })
  }
 let Response = await fetch (url,options);
 let data = await Response.json();
//WORKING
 const calories = data.foods.reduce((acc, item) => acc + item.nf_calories, 0)
 const protein = data.foods.reduce((acc, item) => acc + item.nf_protein, 0)
 const carbs = data.foods.reduce((acc, item) => acc + item.nf_total_carbohydrate, 0)
 const fat = data.foods.reduce((acc, item) => acc + item.nf_total_fat, 0)
 const saturatedfat = data.foods.reduce((acc, item) => acc + item.nf_saturated_fat, 0)
 const sugars = data.foods.reduce((acc, item) => acc + item.nf_sugars, 0)
 const cholesterol = data.foods.reduce((acc, item) => acc + item.nf_cholesterol, 0)
 const fiber = data.foods.reduce((acc, item) => acc + item.nf_dietary_fiber, 0)
 const potassium = data.foods.reduce((acc, item) => acc + item.nf_potassium, 0)
 const sodium = data.foods.reduce((acc, item) => acc + item.nf_sodium, 0)
 //NOT WORKING (NaN)
 const addsugars = data.foods.reduce((acc, item) => acc + item.nf_added_sugars, 0)
 const vita = data.foods.reduce((acc, item) => acc + item.nf_vitamin_a, 0)
 const vitb1 = data.foods.reduce((acc, item) => acc + item.nf_thiamin, 0)
 const vitb2 = data.foods.reduce((acc, item) => acc + item.nf_riboflavin, 0)
 const vitb3 = data.foods.reduce((acc, item) => acc + item.nf_niacin, 0)
 const vitb5 = data.foods.reduce((acc, item) => acc + item.nf_panthothenic_acid, 0)
 const vitb6 = data.foods.reduce((acc, item) => acc + item.nf_vitamin_b6, 0)
 const vitb7 = data.foods.reduce((acc, item) => acc + item.nf_biotin, 0)
 const vitb9 = data.foods.reduce((acc, item) => acc + item.nf_folate, 0)
 const vitb12 = data.foods.reduce((acc, item) => acc + item.nf_vitamin_b12, 0)
 const vitc = data.foods.reduce((acc, item) => acc + item.nf_vitamin_c, 0)
 const vitd = data.foods.reduce((acc, item) => acc + item.nf_vitamin_d, 0)
 const vite = data.foods.reduce((acc, item) => acc + item.nf_vitamin_e, 0)
 const vitk = data.foods.reduce((acc, item) => acc + item.nf_vitamin_k, 0)
 const transfat = data.foods.reduce((acc, item) => acc + item.nf_trans_fat, 0) 
 const monofat = data.foods.reduce((acc, item) => acc + item.nf_monounsaturated_fat, 0)
 const polyfat = data.foods.reduce((acc, item) => acc + item.nf_polyunsaturated_fat, 0)
 const ftype = data.foods.reduce((acc, item) => acc + item.food_type, 0)
 const fcateg = data.foods.reduce((acc, item) => acc + item.food_category, 0)
 const calcium = data.foods.reduce((acc, item) => acc + item.nf_calcium, 0)
 const iron = data.foods.reduce((acc, item) => acc + item.nf_iron, 0)
 const selenium = data.foods.reduce((acc, item) => acc + item.nf_selenium, 0)
 const zinc = data.foods.reduce((acc, item) => acc + item.nf_zinc, 0)
 const magnesium = data.foods.reduce((acc, item) => acc + item.nf_magnesium, 0)
 const phosphorus = data.foods.reduce((acc, item) => acc + item.nf_phosphorus, 0)
 const omega3 = data.foods.reduce((acc, item) => acc + item.nf_omega_3_total, 0)
 const omega6 = data.foods.reduce((acc, item) => acc + item.nf_omega_6_total, 0)
 const aminos = data.foods.reduce((acc, item) => acc + item.nf_amino_acids, 0)
 const lysine = data.foods.reduce((acc, item) => acc + item.nf_lysin, 0)
 const leucine = data.foods.reduce((acc, item) => acc + item.nf_leucine, 0)
 const isoleucine = data.foods.reduce((acc, item) => acc + item.nf_isoleucine, 0)
 const valine = data.foods.reduce((acc, item) => acc + item.nf_valine, 0)
 const threonine = data.foods.reduce((acc, item) => acc + item.nf_threonine, 0)
 const phenylalanine = data.foods.reduce((acc, item) => acc + item.nf_phenylalanine, 0)
 const methionine = data.foods.reduce((acc, item) => acc + item.nf_methionine, 0)
 const histidine = data.foods.reduce((acc, item) => acc + item.nf_histidine, 0)
 const tryptophan = data.foods.reduce((acc, item) => acc + item.nf_tryptophan, 0)
 const betacarotene = data.foods.reduce((acc, item) => acc + item.nf_beta_carotene, 0)
 const lycopene = data.foods.reduce((acc, item) => acc + item.nf_lycopene, 0)
 const lutein = data.foods.reduce((acc, item) => acc + item.nf_lutein, 0)
 const zeaxanthin = data.foods.reduce((acc, item) => acc + item.nf_zeaxanthin, 0)
 const flavonoids = data.foods.reduce((acc, item) => acc + item.nf_flavonoids, 0)
 const polyphenols = data.foods.reduce((acc, item) => acc + item.nf_polyphenols, 0)
 const caffeine = data.foods.reduce((acc, item) => acc + item.nf_caffeine, 0)
 const alcohol = data.foods.reduce((acc, item) => acc + item.nf_alcohol, 0)
 const gi = data.foods.reduce((acc, item) => acc + item.nf_glycemic_index, 0)
 await table.updateRecordAsync(record, {
   'Calories' : calories,
   'Protein' : protein,
   'Total Carbohydrate' : carbs,
   'Total Fat' : fat,
   'Saturated Fat' : saturatedfat,
   'Dietary Fiber' : fiber,
   'Cholesterol' : cholesterol,
   'Sodium' : sodium,
   'Potassium' : potassium,
   'Sugars' : sugars
 })
}

Goal:
My goal is to have all nutritional values written within the //NOT WORKING section of script communicate actual values from the API not NaN.

2

Answers


  1. Sorry if this seems obvious but have you reviewed what is being returned from the API I.e. console.log the data to ensure you’re definitely adding numbers?

    When you add strings with non-numbers it will return NAN

    e.g. 
    1 + 1 = 2 
    '1' + 1 = '11'
    '1a' + 1 = NAN
    
    Login or Signup to reply.
  2. Perhaps the API doesn’t always have every property for every item in the array, at which point your reducer would try to add undefined, resulting in NaN

    Try the following to replace undefined with 0, which will ensure you are only adding numbers

    // notice we have an item in the array without nf_added_sugars
    const apiResponse = [
      { nf_added_sugars: 1 },
      { }, // does not have nf_added_sugars property
      { nf_added_sugars: 2 }
    ];
    
    // we replace null with zero to ensure we only add numbers
    const sugars = apiResponse.reduce((acc,item) => acc + (item.nf_added_sugars ?? 0), 0);
    
    console.log(sugars);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search