Trying to access the nutritionix v1_1 API through a get request has been a bit tricky. Despite the function being called successfully and passing the correct data, the $http.get
request seems to be causing some trouble. It simply skips over the remaining code (the .success
and .error
parts) without returning the promise. I am confident in the request itself, as I have tested it successfully using Postman. The request code looks like this:
(This function is within a factory and is later invoked from a controller.)
let fetchNutrients = (foodId) => {
let authParams = `appId=${NUTRITIONIXAPIKEY.appId}&appKey=${NUTRITIONIXAPIKEY.appKey}`;
let filter = 'fields=item_name,item_id,brand_name,brand_id,item_type,nf_calories,nf_total_carbohydrate,nf_dietary_fiber,nf_cholesterol,nf_total_fat,nf_sugars,nf_sodium,nf_protein,images_front_full_url,nf_serving_size_qty,nf_serving_size_unit,nf_servings_per_container';
return (
$q((resolve,reject) =>{
$http.get(`https://api.nutritionix.com/v1_1/item?id=${foodId}&${filter}&${authParams}`)
.success( (response) => {
console.log('nutritionix response for nutrients request', response);
resolve(response);
}).error(function(errorResponse){
console.log('nutritionix nutrients request failed', errorResponse);
reject(errorResponse);
});
})
);
};
And here is how the factory method is called from the controller:
NutrixFactory.fetchNutrients(foodId).then(function(nutrients){
console.log('returned nutrients', nutrients);
$scope.nutrients.push(nutrients);
console.log('array of nutrients', $scope.nutrients);
});