【问题标题】:How I can access to values inside of this object returned from an API call如何访问从 API 调用返回的该对象内部的值
【发布时间】:2020-07-14 20:06:39
【问题描述】:

Image with the console.log

我想访问和打印对象“命中”的信息

这是我的代码:

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;});
}

【问题讨论】:

标签: javascript reactjs api object


【解决方案1】:

如果您想直接从该函数返回数据,请使用

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));

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多