TypeError: response2.json is not a function
TypeError: response2.json is not a function
我正在尝试使用匿名函数来获取两个数据集。使用两次获取调用第二次调用取决于第一个和第二个是一个循环我一直在第二次调用时出错response2.json不是函数
async function example() {
let response1 = await fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=9c348b767f7a0403e907b0788188afba&text=observatory&accuracy=+11&media=photos+&geo_context=1&lat=41.8989&lon=-87.6123&radius=25&radius_units=km&extras=&format=json&nojsoncallback=1`
);
let json = await response1.json();
console.log(json.photos.photo);
const response2 = await json.photos.photo.map((i) =>
fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=9c348b767f7a0403e907b0788188afba&photo_id=${i.id}&format=json&nojsoncallback=1`
)
);
const json2 = await response2.json();
console.log(json2);
return {
dataset1: json,
dataset2: json2
};
}
问题
response2
是承诺数组,
await json.photos.photo.map
不会使内部请求同步,
const response2 = await json.photos.photo.map((i) =>
fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=9c348b767f7a0403e907b0788188afba&photo_id=${i.id}&format=json&nojsoncallback=1`
)
);
解法:
const promises = json.photos.photo.map((i) =>
fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=9c348b767f7a0403e907b0788188afba&photo_id=${i.id}&format=json&nojsoncallback=1`
)
));
// get all response from promises
const response2 = await Promise.all(promises)
// response2 is array of responses , so we need to loop through it
const json2 = await Promise.all(response2.map(res => res.json()));
const response2 = await json.photos.photo.map(fetch('yourUrl'))
await
后面的是数组,不是promise
所以也许你可以使用 Promise.all 来包装这个数组。
但是fetch
比普通的promise稍微有点特别。
const response2 = await Promise.all(json.photos.photo.map(fetch('yourUrl')));
const responseVirtual = response2.map(res => res.json());
兄弟,希望对你有帮助
我正在尝试使用匿名函数来获取两个数据集。使用两次获取调用第二次调用取决于第一个和第二个是一个循环我一直在第二次调用时出错response2.json不是函数
async function example() {
let response1 = await fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=9c348b767f7a0403e907b0788188afba&text=observatory&accuracy=+11&media=photos+&geo_context=1&lat=41.8989&lon=-87.6123&radius=25&radius_units=km&extras=&format=json&nojsoncallback=1`
);
let json = await response1.json();
console.log(json.photos.photo);
const response2 = await json.photos.photo.map((i) =>
fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=9c348b767f7a0403e907b0788188afba&photo_id=${i.id}&format=json&nojsoncallback=1`
)
);
const json2 = await response2.json();
console.log(json2);
return {
dataset1: json,
dataset2: json2
};
}
问题
response2
是承诺数组,
await json.photos.photo.map
不会使内部请求同步,
const response2 = await json.photos.photo.map((i) =>
fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=9c348b767f7a0403e907b0788188afba&photo_id=${i.id}&format=json&nojsoncallback=1`
)
);
解法:
const promises = json.photos.photo.map((i) =>
fetch(
`https://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=9c348b767f7a0403e907b0788188afba&photo_id=${i.id}&format=json&nojsoncallback=1`
)
));
// get all response from promises
const response2 = await Promise.all(promises)
// response2 is array of responses , so we need to loop through it
const json2 = await Promise.all(response2.map(res => res.json()));
const response2 = await json.photos.photo.map(fetch('yourUrl'))
await
后面的是数组,不是promise
所以也许你可以使用 Promise.all 来包装这个数组。
但是fetch
比普通的promise稍微有点特别。
const response2 = await Promise.all(json.photos.photo.map(fetch('yourUrl')));
const responseVirtual = response2.map(res => res.json());
兄弟,希望对你有帮助