【发布时间】:2018-02-12 08:10:48
【问题描述】:
我正在使用 Yelp API(通过使用 https://cors-anywhere.herokuapp.com/ 作为代理向它发出请求,因为 Yelp API 本身不支持 CORS)来尝试创建一个非常类似于我自己的 Yelp 搜索的应用实践。我能够在浏览器的控制台中获得响应。响应有一个名为业务的数组,其中包含与搜索匹配的业务。我正在尝试使用.(map) 来映射业务数组。但是,我不断收到Cannot read property 'map' of undefined。
我从 Yelp API 收到的回复如下。谢谢 Yelp API Response Image
另外,如果在查看我的代码时有任何其他关于 javascript 的提示,请分享,因为我还处于编程生涯的早期阶段。
const Yelp = {
getAccessToken: function() {
if(accessToken) {
return new Promise(resolve => resolve(accessToken));
};
return fetch(accessTokenUrl, {
method: 'POST'
}).then(response => response.json()).then(jsonResponse => {
accessToken = jsonResponse.access_token;
})
},
search: function(term,location,sortBy) {
return this.getAccessToken().then(() => {
const yelpRetrieveUrl = `${corsAnywhereUrl}https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}&sort_by=${sortBy}`;
return fetch(yelpRetrieveUrl, {
headers: {Authorization: `Bearer ${accessToken}`}
});
}).then(jsonResponse => {
if(1 > 0){
console.log('true!!!!');
return jsonResponse.businesses.map(business => {
return {
id: business.id,
imageSrc: business.image_url,
name: business.name,
address: business.location.address1,
city: business.location.city,
state: business.location.state,
zipCode: business.location.zip_code,
category: business.categories,
rating: business.rating,
reviewCount: business.review_count
};
});
} else {
console.log('FALSE')
}
})
}
};
【问题讨论】:
-
考虑接受已解决的答案?
-
感谢您提及这一点,我没有意识到我需要接受答案。
标签: javascript json ajax fetch-api array.prototype.map