TypeError:无法读取未定义的属性“ append”

时间:2018-07-15 21:13:26

标签: javascript reactjs

尝试在React中提交表单,我在handlesubmit中收到此错误

TypeError: Cannot read property 'append' of undefined
_class.handleSubmit
src/containers/RegisterTenant/register.js:37
  34 | 
  35 | handleSubmit(event){
  36 |   event.preventDefault();
> 37 |   this.data.append("TenantId", this.state.tenantid);
  38 |   this.data.append("TenanrUrl", this.state.tenanturl);
  39 |   this.data.append("TenantPassword", this.state.tenantpassword);

我的代码是

import React, { Component } from 'react';

import { Row, Col } from 'antd';
import PageHeader from '../../components/utility/pageHeader';
import Box from '../../components/utility/box';
import LayoutWrapper from '../../components/utility/layoutWrapper.js';
import ContentHolder from '../../components/utility/contentHolder';
import basicStyle from '../../settings/basicStyle';
import IntlMessages from '../../components/utility/intlMessages';
import { adalApiFetch } from '../../adalConfig';

export default class extends Component {
  constructor(props) {
    super(props);
    this.state = {TenantId: '',TenanrUrl:'',TenantPassword:''};
    this.handleChangeTenantUrl = this.handleChangeTenantUrl.bind(this);
    this.handleChangeTenantPassword = this.handleChangeTenantPassword.bind(this);
    this.handleChangeTenantId= this.handleChangeTenantId.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  };


  handleChangeTenantUrl(event){
    this.setState({tenanturl: event.target.value});
  }

  handleChangeTenantPassword(event){
    this.setState({tenantpassword: event.target.value});
  }

  handleChangeTenantId(event){
    this.setState({tenantid: event.target.value});
  }

  handleSubmit(event){
    event.preventDefault();
    this.data.append("TenantId", this.state.tenantid);
    this.data.append("TenanrUrl", this.state.tenanturl);
    this.data.append("TenantPassword", this.state.tenantpassword);

    const options = {
      method: 'post',
      data: this.data,
      config: {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
    };

    adalApiFetch(options)
      .then(response => response.json())
      .then(responseJson => {
        if (!this.isCancelled) {
          this.setState({ data: responseJson });
        }
      })
      .catch(error => {
        console.error(error);
      });
  }


  upload(e){
      let data = new FormData();
      //Append files to form data
      let files = e.target.files;
      for (let i = 0; i < files.length; i++) {
        data.append('files', files[i], files[i].name);
      }      
  }

  render(){
    const { data } = this.state;
    const { rowStyle, colStyle, gutter } = basicStyle;

    return (
      <div>
        <LayoutWrapper>
        <PageHeader>{<IntlMessages id="pageTitles.TenantAdministration" />}</PageHeader>
        <Row style={rowStyle} gutter={gutter} justify="start">
          <Col md={12} sm={12} xs={24} style={colStyle}>
            <Box
              title={<IntlMessages id="pageTitles.TenantAdministration" />}
              subtitle={<IntlMessages id="pageTitles.TenantAdministration" />}
            >
              <ContentHolder>
              <form onSubmit={this.handleSubmit}>
                <label>
                  TenantId:
                  <input type="text" value={this.state.tenantid} onChange={this.handleChangeTenantId} />
                </label>
                <label>
                  TenantUrl:
                  <input type="text" value={this.state.tenanturl} onChange={this.handleChangeTenantUrl} />
                </label>
                <label>
                  TenantPassword:
                  <input type="text" value={this.state.tenantpassword} onChange={this.handleChangeTenantPassword} />
                </label>
                <label>
                  Certificate:
                  <input onChange = { e => this.upload(e) } type = "file" id = "files" ref = { file => this.fileUpload } />
                </label>             
              <input type="submit" value="Submit" />
              </form>
              </ContentHolder>
            </Box>
          </Col>
        </Row>
      </LayoutWrapper>
      </div>
    );
  }
}

1 个答案:

答案 0 :(得分:2)

您在代码中的任何地方都没有创建this.data,因此this.data将是undefined,并尝试在其中使用append会导致错误。

您可以改为在handleSubmit中创建一个新的FormData

示例

handleSubmit(event){
  event.preventDefault();

  const formData = new FormData();
  formData.append("TenantId", this.state.tenantid);
  formData.append("TenanrUrl", this.state.tenanturl);
  formData.append("TenantPassword", this.state.tenantpassword);

  const options = {
    method: 'post',
    data: formData,
    config: {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  };

  adalApiFetch(options)
    .then(response => response.json())
    .then(responseJson => {
      if (!this.isCancelled) {
        this.setState({ data: responseJson });
      }
    })
    .catch(error => {
      console.error(error);
    });
}