【问题标题】:api call is getting failed after 4 minutes of waiting等待 4 分钟后,api 调用失败
【发布时间】:2019-10-13 17:58:18
【问题描述】:

我正在从客户端进行 API 调用,但等待 4 分钟后失败。

但是当我在 Oracle SQL Developer 中运行相同的查询时,它会花费相同的时间,但会在那里显示数据。

我在前端使用 ReactJS,在后端使用 nodeJS

那么谁能告诉我我应该在前端更改什么,以便 API 调用不会失败并可以呈现数据。

Postman 中也抛出错误 - 等待很长时间后无法得到响应

 componentDidMount() {
    // debugger;
    // event.preventDefault();
    axios.get(`http://localhost:4000/api/AMS/country`)
    .then(response => {
      // console.log(response);
      const country_Claim_Type = response.data;
      this.setState({ country_Claim_Type 

      });
        console.log('data fetched');
        return axios.get(`http://localhost:4000/api/AMS/countryDollar`)
    }).then(response => {
      // console.log(response);
      const country_Claim_Dollar = response.data;
      this.setState({ country_Claim_Dollar 

      });
        console.log('data fetched');
            return axios.get(`http://localhost:4000/api/AMS/claimQuarter`)
        }).then(res => {
              const claims = res.data;
              let claim = [];
              let puru = [];
              claims.forEach(element => {
                claim.push(element.COUNT);  
                puru.push(element.USD);
              });
              this.setState({ 
                Data: {
                  labels: ['FY19 Q1[NOV-JAN]','FY19 Q2[FEB-APR]','FY18[SEP-NOV]'],
                  datasets:[
                     {
                        label:'',
                        data: claim ,puru,
                        backgroundColor:[
                         'rgba(255,105,145,0.6)',
                         'rgba(155,100,210,0.6)',
                         'rgb(63, 191, 191)'

                      ]
                     }
                  ]
               }
               });
           return axios.get(`http://localhost:4000/api/AMS/claimType`)
        }) .then(barGraph => { 
          const claims = barGraph.data;
           let claimcount = [];
          let claimtype = [];
          claims.forEach(element => {
             claimcount.push(element.COUNT);
            claimtype.push(element.CLT_NAME);
          });
          this.setState({ 
            Claim_Type: {
               labels: claimtype,
              datasets:[
                 {
                    label:'',
                    data: claimcount ,
                    backgroundColor:[

                     'rgba(255,105,145,0.6)',
                     'rgba(155,100,210,0.6)',
                     'rgb(255, 0, 64)',
                     'rgb(191, 255, 0)',
                     'rgb(0, 255, 255)',
                     'rgb(128, 0, 255)'
                  ]
                 }
              ]
           }
           });
           return axios.get(`http://localhost:4000/api/AMS/claimCountry`)
        }).then(countryList => {
          const claims = countryList.data;
          let claimcount = [];
          let country = [];
          var coloR = [];
          const percent = "%";
          // let claimvalue = [];
          let claimTotal = 0;
          var dynamicColors = function() {
            var r = Math.floor(Math.random() * 255);
            var g = Math.floor(Math.random() * 255);
            var b = Math.floor(Math.random() * 255);
            return "rgb(" + r + "," + g + "," + b + ")";
         };
          claims.forEach(element => {
           claimTotal+=element.COUNT;
         });
          claims.forEach(element => {
            claimcount.push(parseFloat((element.COUNT/claimTotal)*100).toFixed(2));
            //  claimvalue.push(element.USD);
            country.push(element.COUNTRY);
            coloR.push(dynamicColors());
          });
          this.setState({ 
            country_claim: {
               labels: country,
               datasets:[
                 {
                    label:'ClaimCounts',
                    data: claimcount, 
                    backgroundColor: coloR
                 }
              ]
           }
           });
           return axios.get(`http://localhost:4000/api/AMS/claimDollar`)
          }).then(res => {
            const claims = res.data;
            let dollar = [];
            claims.forEach(element => {
              dollar.push(element.USD);
            });
            this.setState({ 
              dollar_value: {
                labels: ['Q1','Q2','FY18'],
                datasets:[
                   {
                      label:'',
                      data: dollar ,
                      backgroundColor:[
                       'rgba(255,105,145,0.6)',
                       'rgba(155,100,210,0.6)',
                       'rgb(63, 191, 191)'

                    ]
                   }
                ]
             }
             });
            })

  }

【问题讨论】:

标签: node.js reactjs api


【解决方案1】:

如果您的数据库连接正确,并且获取数据需要很长时间,则会导致超时错误。你只需要增加你的请求超时时间,快递是这样的:

req.setTimeout(5 * 60 * 1000);

【讨论】:

  • 好的,但你能告诉我把代码放在我突出显示的 claimQuarter API 部分的什么地方吗?它的中间代码
  • 先增加你的axios超时时间。 axios({ url: '...', timeout: 5 * 60 * 1000 }) 然后,增加你的后端请求超时时间,这取决于你的后端框架。
  • API 调用没有响应,因为它花费了太多时间。我试过你的方法,还是不行。它无法在浏览器中加载数据。
  • 除了我说的,还有一些配置,mybe webpack 代理超时,nginx 超时。我认为您需要找出为什么您的 api 调用需要这么长时间。长时间的api调用可能会导致网络阻塞。默认情况下,chrome 在 HTTP/1.1 中仅允许每个域最多六个连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 2016-05-07
  • 2019-10-15
  • 2018-09-27
  • 1970-01-01
  • 2017-09-17
相关资源
最近更新 更多