Javascript 反应路由获取多个URL
我有一个从API获取JSON数据的路由,并将它发送到我的网页组件Javascript 反应路由获取多个URL,javascript,reactjs,fetch,Javascript,Reactjs,Fetch,我有一个从API获取JSON数据的路由,并将它发送到我的网页组件 app.get('/search-data', (req, res) => { fetch(url) .then(res => res.json()) .then(data => { res.send({ data }); }) .catch(err => { res.redirect('/error') }) 我想发出多个提
app.get('/search-data', (req, res) => {
fetch(url)
.then(res => res.json())
.then(data => {
res.send({ data });
})
.catch(err => {
res.redirect('/error')
})
我想发出多个提取请求。我想我需要使用Promise.all
但是我如何格式化它
我的组件接收一个数据阵列:
async componentDidMount() {
const response = await fetch('/search-data');
const res = await response.json();
...
如果要获取和解析多个URL,然后在单个数组中发送所有结果,则应执行以下操作:
app.get('/search-data', (req, res) => {
let myfetches = [];
myfetches.push(fetch(url1).then(res => res.json()));
myfetches.push(fetch(url2).then(res => res.json()));
myfetches.push(fetch(url3).then(res => res.json()));
Promise.all(myfetches)
.then(datas => {
res.send({ datas });
})
.catch(err => {
res.redirect('/error')
})
这很有效。谢谢你的快速回答。真的很感激!神奇而简单。