【发布时间】:2021-05-22 11:19:10
【问题描述】:
var unirest = require("unirest");
var req = unirest("GET", "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data");
req.query({
"ingr": "1 large apple"
});
req.headers({
"x-rapidapi-host": "HOST",
"x-rapidapi-key": "KEY",
"useQueryString": true
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.body);
});
我正在尝试使用该文档和参数进行 API 调用,以便根据搜索参数获取成分列表。
这是我的服务:
async getAllNutrients(ingr: string) {
console.log(ingr);
const headersRequest = {
'x-rapidapi-host': 'edamam-edamam-nutrition-analysis.p.rapidapi.com',
'x-rapidapi-key': '5664b75c9fmsh66ac8e054422eb9p1600b8jsn878d097e8d2a',
useQueryString: true,
};
const result = await this.httpService.get(
`https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data` +
ingr,
{ headers: headersRequest },
);
console.log(result);
return result;
}
这是我的控制器
@Get('/list?:ingr')
getMacros(@Query('ingr') ingr) {
return this.macroService.getAllNutrients(ingr);
}
我尝试更改 QUEery 和 Param,但都没有工作。 在邮递员上,我进行了这样的 API 调用: "localhost:3000/macros/list?ingr="1 个大苹果" 我的 2 console.log 返回:
"1 large apple"
Observable { _isScalar: false, _subscribe: [Function] }
[Nest] 7460 - 2020-09-21 16:00:55 [ExceptionsHandler] Request failed with status code 404 +441782ms
Error: Request failed with status code 404
我尝试像这个例子一样使用管道:
getQuote(id){
return this.http.get('http://quotesondesign.com/wp-json/posts/' + id)
.pipe(
map(response => response.data)
);
}
但结果是一样的。有什么帮助吗?
【问题讨论】:
标签: node.js mongodb typescript nestjs