简体   繁体   中英

Angular $http.get request not running success or error functions

I am trying to make a get request to the nutritionix v1_1 api. When debugging I can see that the function is successfully called and the correct data is passed in. When the function hits the $http.get request it skips over the rest of the code(the .success and .error parts), and it doesn't return the promise. I know the request is good because I have made successful requests using postman. I have the request written like:

(This method is inside of a factory. It is later called from a controller.)

let getNutrients = (foodId) => {
    let authPart =`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';
    // let sort = 'sort: {"field":"_score", "order":"desc"}';
    // let typefilter = '"filters":{"not": {"item_type":3}}';
    return (
        $q((resolve,reject) =>{             
            $http.get(`https://api.nutritionix.com/v1_1/item?id=${foodId}&${filter}&${authPart}`)
            .success( (response) => {
                console.log('nutrix response nutrients request', response);
                resolve(response);
            }).error(function(errorResponse){
                console.log('nutrix fail nutrients request', errorResponse);
                reject(errorResponse);
            });
        })
    );
};

here is the factory method call from the controller:

NutrixFactory.getNutrients(foodId).then(function(nutrients){
    console.log('nutrients returned', nutrients);
    // $scope.nutrients = $scope.nutrients || [];
    $scope.nutrients.push(nutrients);
    console.log('nutrients array', $scope.nutrients);
});

try then and catch instated of success and error if its 1.6.*

Deprecation Notice

let getNutrients = (foodId) => {
    let authPart =`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';
    // let sort = 'sort: {"field":"_score", "order":"desc"}';
    // let typefilter = '"filters":{"not": {"item_type":3}}';
    return (
        $q((resolve,reject) =>{             
           return $http.get(`https://api.nutritionix.com/v1_1/item?id=${foodId}&${filter}&${authPart}`)
            .then( (response) => {
                console.log('nutrix response nutrients request', response);
                resolve(response);
            }).catch(function(errorResponse){
                console.log('nutrix fail nutrients request', errorResponse);
                reject(errorResponse);
            });
        })
    );
};

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