【发布时间】:2020-10-14 02:50:57
【问题描述】:
我正在努力从 API 调用中提取数据。我需要它来返回一个键数组,但是当我尝试渲染时,我不断收到[object Promise]。
const apiKey = '*****';
const container = document.getElementById('root');
const Yelp = {
async search(term, location, searchBy) {
let response = await fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}&sort_by=${searchBy}`, {
headers: {
Authorization: `Bearer ${apiKey}`
}
})
if (response.ok) {
let jsonResponse = await response.json()
return jsonResponse.businesses.map(business => {
return business
})
}
}
}
console.log(Yelp.search('pasta', 'london', 'best_match'));
【问题讨论】:
-
Yelp.search是async因此返回一个 Promise .. 您可以等待该承诺(如果您在async函数中这样做)或使用承诺的.then方法来访问承诺的解决值 -
Yelp.search().then() ... 在 then() 中你会得到我认为的返回数据...
标签: javascript ajax api async-await