gpt4 book ai didi

javascript - 无法读取未定义的属性 'tenantid'

转载 作者:行者123 更新时间:2023-12-03 01:29:52 27 4
gpt4 key购买 nike

我正在尝试创建一个简单的 react 表单来将数据提交到 webapi,但是我收到此错误

Unhandled Rejection (TypeError): Cannot read property 'tenantid' of undefined
./src/containers/RegisterTenant/register.js
src/containers/RegisterTenant/register.js:13
10 | import { adalApiFetch } from '../../adalConfig';
11 |
12 | const data = {
> 13 | TenantId: this.state.tenantid,
14 | TenanrUrl: this.state.tenanturl,
15 | TenantPassword: this.state.tenantpassword
16 | };

我的代码是这样的:

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';

const data = {
TenantId: this.state.tenantid,
TenanrUrl: this.state.tenanturl,
TenantPassword: this.state.tenantpassword
};

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

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){
alert('A name was submitted: ' + this.state.value);
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>
);
}
}

最佳答案

这是一个范围的问题。在您的示例中, this.state 未定义,因为 this 指的是全局范围,或者如果您处于严格模式,则为 undefined 。需要将其移至组件的内部,以便将this 设置为组件的实例。您可以使用 class instance property ,或将其移动到类构造函数中。

您的 render() 函数内部还有另一个问题,您正在解构 this.state 并获取 data 值,但是this.state 上没有 data 键。请参阅构造函数。

关于javascript - 无法读取未定义的属性 'tenantid',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51351846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com