尝试映射 Yelp API 响应
Trying to Map Yelp API repsonse
我正在使用 Yelp API(通过使用 https://cors-anywhere.herokuapp.com/ 作为代理向它发出请求——因为 Yelp API 本身不是t CORS-enabled) 尝试创建一个非常类似于 Yelp 搜索我自己的实践的应用程序。我能够将响应输入到浏览器的控制台中。响应有一个名为 businesses 的数组,其中包含与搜索匹配的企业。我正在尝试使用 .(map)
来映射 businesses 数组。但是,我不断收到 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')
}
})
}
};
fetch(myRequest).then(function(response) {
var contentType = response.headers.get("content-type");
if(contentType && contentType.includes("application/json")) {
return response.json();
}
throw new TypeError("Oops, we haven't got JSON!");
})
.then(function(json) { /* process your JSON further */ })
.catch(function(error) { console.log(error); });
您可以搜索 jsonResponse#json 函数,将您的 json 数据集转换为您可以在 JavaScript 中处理的真实对象。
并且因为您要求它: 当您使用 Promises 时,请使用 Promise#Catch 函数来处理即将发生的错误。不要让浏览器处理它们,因为它们在不同的浏览器中可能有不同的行为。
并且可能会删除 1 > 0 检查,因为它始终为真,但我认为这仅用于测试目的。
希望能帮到你!我可能会稍后附加代码,因为我目前在移动设备上。
我正在使用 Yelp API(通过使用 https://cors-anywhere.herokuapp.com/ 作为代理向它发出请求——因为 Yelp API 本身不是t CORS-enabled) 尝试创建一个非常类似于 Yelp 搜索我自己的实践的应用程序。我能够将响应输入到浏览器的控制台中。响应有一个名为 businesses 的数组,其中包含与搜索匹配的企业。我正在尝试使用 .(map)
来映射 businesses 数组。但是,我不断收到 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')
}
})
}
};
fetch(myRequest).then(function(response) {
var contentType = response.headers.get("content-type");
if(contentType && contentType.includes("application/json")) {
return response.json();
}
throw new TypeError("Oops, we haven't got JSON!");
})
.then(function(json) { /* process your JSON further */ })
.catch(function(error) { console.log(error); });
您可以搜索 jsonResponse#json 函数,将您的 json 数据集转换为您可以在 JavaScript 中处理的真实对象。
并且因为您要求它: 当您使用 Promises 时,请使用 Promise#Catch 函数来处理即将发生的错误。不要让浏览器处理它们,因为它们在不同的浏览器中可能有不同的行为。
并且可能会删除 1 > 0 检查,因为它始终为真,但我认为这仅用于测试目的。
希望能帮到你!我可能会稍后附加代码,因为我目前在移动设备上。