如何从 node.js 函数中获取 API 数据
how to get API data out of a node.js function
我对 JS 很陌生,对 Node.js 也很陌生。我正在尝试制作一个非常简单的应用程序,从 edamam API (https://rapidapi.com/edamam/api/edamam-nutrition-analysis) 中提取 JSON 数据。我已经让 API 工作了,我现在想使用请求 return 的数据,但它卡在回调函数中,我无法弄清楚如何在外部访问它检索它的函数。
据我所知,这与 Node.js 的异步特性有关。我已经尝试以我能想到的所有方式重新排序(超过 6 小时!),但我得到的只是未定义的变量。我想那是因为它们还没有设置,但我对回调函数的处理还不够了解如何解决这个问题。
最终我希望能够使用 JSON 数据来更新 HTML 元素的内容。
const unirest = require('unirest');
var getNutritionInfo = {
mkString: function(foodStr){
var ingredients = new Array();
ingredients.push(foodStr);
console.log(foodStr)
params = {
ingr: ingredients,
};
esc = encodeURIComponent;
query = Object.keys(params)
.map(k => esc(k) + '=' + esc(params[k]))
.join('&');
foodQuery = query.replace(/%20/g,"+");
return(foodQuery);
},
analyse: function(foodStr){
var cals;
url = "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data"
searchQuery = getNutritionInfo.mkString(foodStr);
function requestData(callback){
unirest.get(url+"?" + searchQuery)
.header("X-RapidAPI-Key", "c5c219c7b0mshbf5b602f68caddep1cd8cfjsn01a1be1f45a4")
.end(function (result) {
cals = result.body.totalNutrients.ENERC_KCAL.quantity;
console.log(cals + " first");
callback();
});
}
requestData(function(){
console.log(cals + " second");
getNutritionInfo.getData();
});
},
getData: function(){
var calories = getNutritionInfo.analyse.cals;
console.log(calories + " third");
return calories;
}
};
最终的 console.log 始终未定义,而我希望它 return cals (result.body.totalNutrients.ENERC_KCAL.quantity);
行上的值
另外,非常抱歉。我知道必须有更简洁的方法来组织函数和格式化代码。我最终会到达那里:)
the final console.log is ALWAYS undefined, whereas I am wanting it to return the value on line of cals (result.body.totalNutrients.ENERC_KCAL.quantity);
这是因为nodejs的异步特性。要从 requestData()
获取值,您需要将 cal
作为值传递给回调。
进行这些更改,
'use strict';
const unirest = require('unirest');
var getNutritionInfo = {
mkString: function (foodStr) {
var ingredients = new Array();
ingredients.push(foodStr);
console.log(foodStr)
params = {
ingr: ingredients,
};
esc = encodeURIComponent;
query = Object.keys(params)
.map(k => esc(k) + '=' + esc(params[k]))
.join('&');
foodQuery = query.replace(/%20/g, "+");
return (foodQuery);
},
analyse: function (foodStr) {
url = "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data"
searchQuery = getNutritionInfo.mkString(foodStr);
function requestData(callback) {
unirest.get(url + "?" + searchQuery)
.header("X-RapidAPI-Key", "c5c219c7b0mshbf5b602f68caddep1cd8cfjsn01a1be1f45a4")
.end(function (result) {
let cals = result.body.totalNutrients.ENERC_KCAL.quantity;
console.log(cals + " first");
callback(cals); // pass value here
});
}
requestData(function (cals) { // get value after completion
console.log(cals + " second");
getNutritionInfo.getData();
});
},
getData: function () {
var calories = getNutritionInfo.analyse.cals;
console.log(calories + " third");
return calories;
}
};
我对 JS 很陌生,对 Node.js 也很陌生。我正在尝试制作一个非常简单的应用程序,从 edamam API (https://rapidapi.com/edamam/api/edamam-nutrition-analysis) 中提取 JSON 数据。我已经让 API 工作了,我现在想使用请求 return 的数据,但它卡在回调函数中,我无法弄清楚如何在外部访问它检索它的函数。
据我所知,这与 Node.js 的异步特性有关。我已经尝试以我能想到的所有方式重新排序(超过 6 小时!),但我得到的只是未定义的变量。我想那是因为它们还没有设置,但我对回调函数的处理还不够了解如何解决这个问题。
最终我希望能够使用 JSON 数据来更新 HTML 元素的内容。
const unirest = require('unirest');
var getNutritionInfo = {
mkString: function(foodStr){
var ingredients = new Array();
ingredients.push(foodStr);
console.log(foodStr)
params = {
ingr: ingredients,
};
esc = encodeURIComponent;
query = Object.keys(params)
.map(k => esc(k) + '=' + esc(params[k]))
.join('&');
foodQuery = query.replace(/%20/g,"+");
return(foodQuery);
},
analyse: function(foodStr){
var cals;
url = "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data"
searchQuery = getNutritionInfo.mkString(foodStr);
function requestData(callback){
unirest.get(url+"?" + searchQuery)
.header("X-RapidAPI-Key", "c5c219c7b0mshbf5b602f68caddep1cd8cfjsn01a1be1f45a4")
.end(function (result) {
cals = result.body.totalNutrients.ENERC_KCAL.quantity;
console.log(cals + " first");
callback();
});
}
requestData(function(){
console.log(cals + " second");
getNutritionInfo.getData();
});
},
getData: function(){
var calories = getNutritionInfo.analyse.cals;
console.log(calories + " third");
return calories;
}
};
最终的 console.log 始终未定义,而我希望它 return cals (result.body.totalNutrients.ENERC_KCAL.quantity);
行上的值另外,非常抱歉。我知道必须有更简洁的方法来组织函数和格式化代码。我最终会到达那里:)
the final console.log is ALWAYS undefined, whereas I am wanting it to return the value on line of cals (result.body.totalNutrients.ENERC_KCAL.quantity);
这是因为nodejs的异步特性。要从 requestData()
获取值,您需要将 cal
作为值传递给回调。
进行这些更改,
'use strict';
const unirest = require('unirest');
var getNutritionInfo = {
mkString: function (foodStr) {
var ingredients = new Array();
ingredients.push(foodStr);
console.log(foodStr)
params = {
ingr: ingredients,
};
esc = encodeURIComponent;
query = Object.keys(params)
.map(k => esc(k) + '=' + esc(params[k]))
.join('&');
foodQuery = query.replace(/%20/g, "+");
return (foodQuery);
},
analyse: function (foodStr) {
url = "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data"
searchQuery = getNutritionInfo.mkString(foodStr);
function requestData(callback) {
unirest.get(url + "?" + searchQuery)
.header("X-RapidAPI-Key", "c5c219c7b0mshbf5b602f68caddep1cd8cfjsn01a1be1f45a4")
.end(function (result) {
let cals = result.body.totalNutrients.ENERC_KCAL.quantity;
console.log(cals + " first");
callback(cals); // pass value here
});
}
requestData(function (cals) { // get value after completion
console.log(cals + " second");
getNutritionInfo.getData();
});
},
getData: function () {
var calories = getNutritionInfo.analyse.cals;
console.log(calories + " third");
return calories;
}
};