javascript - 如何访问从 API 调用返回的该对象内部的值
问题描述
我想访问和打印对象“命中”的信息
这是我的代码:
export async function fetchData(searchValue) {
await fetch(`https://api.edamam.com/search?q=${searchValue}&app_id=${apiId}&app_key=${apiKey}`)
.then(response => response.json())
.then((res) => {return res;});
}
解决方案
如果您想直接从该函数返回数据,请使用
export async function fetchData(searchValue) {
return await fetch(
`https://api.edamam.com/search?q=${searchValue}&app_id=${apiId}&app_key=${apiKey}`
)
.then((response) => response.json());
}
现在调用该方法时,要么使用 async/await 要么.then
记录
// async/await (inside of an async function)
const result = await fetchData('something');
console.log(result.hits);
// `.then`
fetchData('something').then(result => console.log(result.hits));
推荐阅读
- r - 当您已经给出百分比分布时,在 r 中制作小提琴图
- c++ - 通过从向量中获取键和与映射中的键对应的值来创建映射
- excel - MS Excel:是否有更简单的方法来使用 IF 语句检查匹配数据并输出日期?
- tcl - 检测移位+数字的键盘独立方式
- scala - 添加基于合规性分析器的检查
- python - Pandas 输出和数据类型
- python - 有条件地搜索嵌套字典 Python 的更快方法
- node.js - 节点快递服务器未使用 aws lambda 为 Angular 应用程序呈现静态内容
- c++11 - Eignen 矩阵状态向量对一些样本进行平均以进行方向计算
- python - Python - regex.match 和不同的文本编解码器?