【问题标题】:Capture the response of async axios call in react在 react 中捕获异步 axios 调用的响应
【发布时间】:2020-08-01 04:56:04
【问题描述】:
 async  saveForm(data) {
     return await axios({
       method: "post",
       url: FORM_PORTAL_CONTROLLER_URL + 'savemoratorium',
       body: JSON.stringify(data)
    }).then(res => 
        this.setState({ refNo: res.data.reference }))
  }

我无法设置我从请求中获得的设置状态,它给了我以下错误。 Post 请求完美执行。我想捕获响应并更改 state.function 在组件内。以下错误出现在页面的控制台中,之后我得到一个白页。

react_devtools_backend.js:6 TypeError: Cannot read property 'length' of undefined react-dom.production.min.js:152 Uncaught (in promise) TypeError: Cannot read property 'length' of undefined

【问题讨论】:

  • 在axios请求中将body替换为data

标签: html reactjs async-await axios


【解决方案1】:

我已经找到了这个案例的根本原因。这是因为我与材质 ui 节点模块发生冲突。 (makeEffect,useEffect)

【讨论】:

    【解决方案2】:

    在渲染前添加验证

    // Sample 1
    
          if(Array.isArray(refNo) && refNo.length){ 
            //your code
           }
    
    //Sample 2
    
          {Array.isArray(refNo) && refNo.length? customRenderFunction(): null}
    

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 2020-12-09
      • 1970-01-01
      • 2015-04-26
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多