首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义不是react本机中的对象(计算'Amazon.trim')

未定义不是react本机中的对象(计算'Amazon.trim')
EN

Stack Overflow用户
提问于 2021-03-07 15:07:11
回答 1查看 51关注 0票数 0

我对原生反应还很陌生。我已经创建了一个表单。但现在它不会将数据发送到服务器。当我点击Submit按钮时。它一直像这样抛出错误

代码语言:javascript
复制
 undefined is not an object (evaluating 'Amazon.trim')

那么我的代码出了什么问题,请帮帮忙。以下是我的代码

代码语言:javascript
复制
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按钮时。它一直像这样抛出错误

EN

回答 1

Stack Overflow用户

发布于 2021-03-07 15:14:26

看起来你在useState中将亚马逊设置为未定义,然后尝试访问它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66514062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档