程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决React Native Uncaught (in promisE) SyntaxError: Unexpected token < in JSON at position 0?

开发过程中遇到React Native Uncaught (in promisE) SyntaxError: Unexpected token < in JSON at position 0的问题如何解决?下面主要结合日常开发的经验,给出你关于React Native Uncaught (in promisE) SyntaxError: Unexpected token < in JSON at position 0的解决方法建议,希望对你解决React Native Uncaught (in promisE) SyntaxError: Unexpected token < in JSON at position 0有所启发或帮助;

我在 React Native 中的代码有一个小问题,错误说 Uncaught (in promisE) SyntaxError: Unexpected token ,我只是想用新密码更新现有密码。

  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,})
      .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={{userdata:UserData.password,password1: "",password2: "" }}
          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="@R_758_2403@"
            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="@R_758_2403@"
            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="@R_758_2403@"
            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>

这是API的返回

React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

这是我从 console.log 收到的错误

未捕获(承诺)语法错误:JsON 中的意外标记

错误指向这里

React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

console.log("_data",_data)

React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

这是来自 API 的响应

React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

API

async function updateUserpassword(req,res,next){
    params = req.body;//post
    let valIDation_rules = {
        UsersID:'required|Integer',password: 'required'
    }
    console.log("valIDation_rules",valIDation_rules);
    let valIDation = new ValIDator(params,valIDation_rules);
    if(valIDation.passes())
    {
        try
        {
            let resultsplayer = await userprofile.getProfile(params)
            if(resultsplayer.rowCount > 0){
                let results = await userprofile.updateUserpassword(params)
                if(results.rowCount > 0){
                    res.status(200).Json(results.rows);
                }else{
                    res.status(304).Json({"message":"update Failed"});
                }
            }else{
                res.status(404).Json({"message":"Record Not Found"});
            }
        }catch(err){
            console.log("-----result data error-----\n",err);
            res.status(500).Json(err);
        }
    }else{
        res.status(400).Json(valIDation.errors);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0全部内容,希望文章能够帮你解决React Native Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。