I have a little problem in my code in React Native, the error says Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 , I just want to update the existing password with new password.
const getProfile = async(token) =>{ let jsonValue = await AsyncStorage.getItem('@storage_Key') jsonValue = JSON.parse(jsonValue); setProfile(jsonValue); console.log(config.API_URL + '/users/'+ jsonValue.userId) fetch(config.API_URL + '/users/'+ jsonValue.userId, { method: 'GET', headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json', }, }) .then((response) => response.json().then(data=>({status:response.status, body:data})) ) .then((response) => { console.log("User information = ",response); if(response.status==200) { if(response.body.length > 0) { let _UserData = {}; setUserData(response.body[0]); setUserPassword(decodeURI(response.body[0].Password)) console.log("response.body[0]",response.body[0]); }else{ setErrortext(response.body.msg); setPreventSubmit(true); } }else{ setErrortext(response.body.msg); setPreventSubmit(true); }; }) } const handleSubmit = async(valuesToSubmit) => { let finalValidationPassed=true; if(valuesToSubmit['password1'] == valuesToSubmit['password2'] & valuesToSubmit['password2'] == valuesToSubmit.userdata){ let _data = { UsersId: UserData.UsersId, Password: valuesToSubmit.userdata } console.log("_data",_data) if(finalValidationPassed) { setErrortext(""); fetch(config.API_URL + '/user/password', { method: 'PUT', body: JSON.stringify(_data), headers: { 'Authorization': 'Bearer '+ token, 'Content-Type': 'application/json', }, }) .then((response) => response.json().then(data=>({status:response.status, body:data})) ) .then((response) => { console.log("data",response); if(response.status==200){ setIsEdit(false); alert("Profile Updated!"); refetch(); }else{ setErrortext(response.body[0]?.msg); setErrortext(response.body[0]?._msgerror); } }); }else{ alert("Error"); } }else{ alert("Sorry, submission failed. Please check your data entries."); } } <Formik enableReinitialize initialValues= onSubmit={(values) =>{ handleSubmit(values)}} > {(props) => ( <SafeAreaView> <View style={formStyles.SectionStyle}> <Text>CURRENT PASSWORD</Text> <TextInput name="Password" value={props.values.userdata} style={formStyles.inputStyle} underlineColorAndroid="#f000" placeholderTextColor="#8b9cb5" keyboardType="email-address" secureTextEntry={true} onChangeText={props.handleChange('userdata')} /> </View> {(props.errors.Password && props.touched.Password) && <Text style={formStyles.errorText}>{errors.Password}</Text> } <View style={formStyles.SectionStyle}> <Text>SET NEW PASSWORD</Text> <TextInput name="password1" value={props.values.password1} style={formStyles.inputStyle} underlineColorAndroid="#f000" placeholderTextColor="#8b9cb5" keyboardType="email-address" onChangeText={props.handleChange("password1")} /> </View> {(props.errors.Password && props.touched.Password) && <Text style={formStyles.errorText}>{errors.Password}</Text> } <View style={formStyles.SectionStyle}> <Text>CONFIRM NEW PASSWORD</Text> <TextInput name="password2" value={props.values.password2} style={formStyles.inputStyle} underlineColorAndroid="#f000" placeholderTextColor="#8b9cb5" keyboardType="email-address" onChangeText={props.handleChange("password2")} /> </View> {(props.errors.Password && props.touched.Password) && <Text style={formStyles.errorText}>{errors.Password}</Text> } <TouchableOpacity style={formStyles.buttonStyle} activeOpacity={0.5} disabled={!props.isValid} onPress={props.handleSubmit} > <Text style={formStyles.buttonTextStyle}>RESET PASSWORD</Text> </TouchableOpacity> </SafeAreaView> )} </Formik>
this is the return
this is the error i recieved from the console.log
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
the error is pointing here
https://stackoverflow.com/questions/67249064/react-native-uncaught-in-promise-syntaxerror-unexpected-token-in-json-at-p April 25, 2021 at 09:56AM
没有评论:
发表评论