我正在尝试向nutrition病毒v1_1 api提出请求。调试时,我可以看到函数被成功调用并传入了正确的数据。当函数点击$http.get
请求时,它会跳过其余的代码(.success
和{{1} }部分),它不会返回承诺。我知道请求很好,因为我已经使用邮递员提出了成功的请求。我的请求写得像:
(此方法在工厂内部。稍后从控制器调用。)
.error
这是来自控制器的工厂方法调用:
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);
});
})
);
};
答案 0 :(得分:2)
尝试然后捕获成功和错误,如果它的1.6。*
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);
});
})
);
};