我正在尝试编写一些代码,以获取API响应,使用该数据插入新的API调用,并获取响应,并执行一些逻辑以正确的顺序将其返回以显示给用户。
makeRequest = async () => {
let realPhotoData = [];
let actualPhotoData = [];
let actualCityData = [];
const place = this.state.inputvalue;
const url = url;
// first fetch call returning 8 venues for location searched (picture data not included in response)
fetch(url)
.then(response => {
return response.json();
})
.then(data => {
let allCities = data.response.groups[0].items;
for (let key of allCities) {
let searchData = {
title: key.venue.name,city: key.venue.location.city,country: key.venue.location.country,photoId: key.venue.id
};
let venueIdArray = {
photoId: key.venue.id
};
actualCityData.push(searchData);
actualPhotoData.push(venueIdArray);
} /* end of for loop,placed response data in 2 arrays,need venueIdArray for 2nd fetch call (loop of 8 fetch calls) */
// for loop for second fetch call(8 of them) it returns photos for 8 venues
for (let i = 0; i < actualPhotoData.length; i++) {
var venueId = actualPhotoData[i].photoId;
let photoUrl = photoUrl;
let callPhotoSearch = async (actualCityData) => {
if (actualCityData) {
const photoRequest = fetch(photoUrl)
.catch(err => {
return { error: err };
})
await Promise.all([photoRequest]).then(data => {
const photoData = data.json();
// })
// .then(data => {
realPhotoData.push({
returnPhotoId: photoData.meta.requestId,returnVenueId: i,prefix: photoData.response.photos.items[0].prefix,size: "300x500",suffix: photoData.response.photos.items[0].suffix
});
console.log(realPhotoData)
realPhotoData.sort(
(a,b) => a.returnVenueId - b.returnVenueId
);
var photoAndSearchData = [];
for (let j = 0; j < actualCityData.length; j++) {
// console.log(realPhotoData,actualCityData);
photoAndSearchData.push({
key1: actualCityData[j],key2: realPhotoData[j]
});
console.log(photoAndSearchData);
this.setState({
cardArray: photoAndSearchData
});
}
console.log(this.state);
});
}
};
callPhotoSearch(actualCityData);
}
});
};
不幸的是,我不断收到Unhandled Rejection (TypeError): data.json is not a function
,并且不确定当我需要JSON格式的数据时如何解决。