我得到了以下錯誤:
我得到了以下錯誤。
React JS Unhandled Rejection(TypeError)。專案。 persistVariablesLimit.trim不是一個function。
當我試圖在 persistVariablesLimit 上設定一個默認值時。
以下是堆疊跟蹤和控制臺日志的影像:
下面是生成它的代碼:
if (typeof item.persistVariablesLimit !=='undefined') {
item.persistVariablesLimit = item.persistVariablesLimit.trim()
if (!item.persistVariablesLimit.match(/^[0-9] $/) {
formIsValid = false[/span
errors['persistVariablesLimit'] = '必須是數字'。
}
}
如果手動輸入資料,這個欄位是沒有問題的,即使輸入后被洗掉也是如此。
另外,如果我不設定默認值,并且不在該欄位中輸入任何東西,它就會被成功保存為一個空字串。
這里是設定默認值的地方(當 "FA "模板被選中時):
handleSelectTemplateChange = (event, { value }) => {
let item = { ...this.state.item }
item.template = value
var str1 = '' item.template = value
if (str1.startsWith('SMA') {
item.family='Safety-Machine-Analytics'。
}
if (str1.startsWith('FA') {
item.family='Field Analytics'
item.product='Field Analytics'
if (!item.persistVariablesLimit) {
item.persistVariablesLimit = 50;
}
if (!item.dataSourceLimit) {
item.dataSourceLimit = 50;
}
}
else {
item.persistVariablesLimit = ""/span>;
item.dataSourceLimit = "。
}
this.setState({ item })
}
這是該模板的UI代碼:
<Form.Select.
流動性
label='Template'。
options={this.state.templateOptions|[]}
placeholder='Template'。
name='模板'/span>
value={item.template}。
required={true}。
onChange={this.handleSelectTemplateChange}。
/>
而對于 persistVariableLimit 欄位:
<Form.Input.
label='Persist Variables Limit'。
placeholder='持久變數限制'。
name='persistVariablesLimit'/span>
value={item.persistVariablesLimit || ''}。
onChange={this.handleChange}。
required={false}。
disabled={false}。
error={this.state.errors['persistVariablesLimit']} 。
/>。
這是一個最初從API中獲取的專案。它的初始化程序如下:
emptyItem = {
fullName: ''/span>,
contact: '',
requester: '',
tag: '',
company: '',
companyId: '',
product: '',
有效期: '',
macsArray: '',
dashboardIDs: '',
typeId: '',
family: '',
systems: '',
fileName: '',
url: '',
attributesArray: [],
persistVariablesLimit: ''/span>,
dataSourceLimit: '', dataSourceLimit.
}
constructor(props) {
super(props)
const { cookies } = props
this.state = {
item: this.emptyItem。
csrfToken: cookies.get('XSRF-TOKEN') 。
fields: {},
errors: {}.
}
...
}
這里是API呼叫:
if (this.props。 match.params.id !== 'new') {
try {
const tmpLicense = await (await fetch(API_HOST `/api/license/${this. props.match.params.id}`, { credentials: 'include' }).json()
this.setState({ item: tmpLicense })
} catch (error) {
this.props.history.push('/')
}
我應該如何設定這個默認值?這里的問題是什么?
uj5u.com熱心網友回復:
你將默認值設定為數字,.trim是一個字串方法。
它應該是:
if (!item.persistVariablesLimit) {
item.persistVariablesLimit = '50'。
}
if (!item.dataSourceLimit) {
item.dataSourceLimit = '50';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/316175.html
標籤:

