Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应路由获取多个URL_Javascript_Reactjs_Fetch - Fatal编程技术网

Javascript 反应路由获取多个URL

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') }) 我想发出多个提

我有一个从API获取JSON数据的路由,并将它发送到我的网页组件

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')
        })

这很有效。谢谢你的快速回答。真的很感激!神奇而简单。