未处理的拒绝(TypeError):无法读取未定义的属性“ catch”

时间:2019-05-08 07:39:10

标签: reactjs rest

我正在使用以下功能将数据从前端传递到后端

 async componentDidMount(){
 let data = this.state.i
 const addFormData = await actions.addFormData(data).catch(e => console.log(e));
 console.log("this is i for actions");
 this.setState({rows: addFormData})
 }

输入的表单数据存储在以下名为 values

    handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
        this.setState({
          i:values
        }, () => console.log(this.state.i))
      }
    });
  }

我得到这个error 我能知道错误在哪里吗?由于我是react的后端部分的新手,在其中传递api

1 个答案:

答案 0 :(得分:2)

您需要将其放在try catch块中

尝试一下:

async componentDidMount(){
 let data = this.state.i;
    try {
         const addFormData = await actions.addFormData(data);
         if(!addFormData ) return console.log("this is i for actions");
         this.setState({rows: addFormData})
    } catch(e) {
        return console.log(e);
    }
}