我对原生反应还很陌生。我已经创建了一个表单。但现在它不会将数据发送到服务器。当我点击Submit按钮时。它一直像这样抛出错误
undefined is not an object (evaluating 'Amazon.trim')那么我的代码出了什么问题,请帮帮忙。以下是我的代码
export default function Add(props) {
const { navigation } = props
const data = props.route.params.data
const amazonD = data.service_details_data[0] ? data.service_details_data[0].amazon_name : ''
const [AmazonError, setAmazonError] = useState([]);
const [Amazon, setAmazon] = useState(undefined);
const validateInputs = () => {
if (!Amazon.trim()) {
setAmazonError('Please Fill The Input')
return;
}
else
{
//+++++++++++++++++++++++++++++++++=submitting form data to api start+++++++++++++++++++++++++++++++++++
{
const data = props.route.params.data
const phpid = data.service_details_data[0] ? data.service_details_data[0].id : ''
AsyncStorage.multiGet(["application_id", "created_by",'leadTagNumber']).then(response => {
fetch('https://xyztech/Android_API_CI/uploaddata/merchant_service_details', {
method: 'POST',
headers: {'Accept': 'application/json, text/plain, */*', "Content-Type": "application/json" },
// We convert the React state to JSON and send it as the POST body
body: JSON.stringify([{ some data}])
})
.then((returnValue) => returnValue.json())
.then(function(response) {
console.log(response)
Alert.alert("File uploaded");
return response.json();
});
});
// event.preventDefault();
}
//+++++++++++++++++++++++++++++++++submitting form data to api end++++++++++++++++++++++++++++++++++++++
Alert.alert("success")
return;
}
};
const handleAmazon = (text) => {
setAmazonError('')
setAmazon(text)
}
return (
<View style={{flex: 1}}>
<ScrollView style={{flex: 1,}} showsVerticalScrollIndicator={false}>
<TextInput
maxLength={30}
placeholder="Amazon *"
style={styles.inputStyle}
onChangeText={(text)=>handleAmazon(text)}
// value={Amazon}
defaultValue={amazonD}
value = {Amazon} />
<Text style={{color :'red'}}>{AmazonError}</Text>
</ScrollView>
<Button
style={styles.inputStyleB}
title="Submit"
color="#FF8C00"
onPress={() => validateInputs()}
/>
</View>
)
}请忽略这一点=我对原生反应很陌生。我已经创建了一个表单。但现在它不会将数据发送到服务器。当我点击Submit按钮时。它一直像这样抛出错误
发布于 2021-03-07 15:14:26
看起来你在useState中将亚马逊设置为未定义,然后尝试访问它。
https://stackoverflow.com/questions/66514062
复制相似问题