【问题标题】:how to store accesstoken from API to asyncstorage如何将访问令牌从 API 存储到异步存储
【发布时间】:2019-05-23 10:29:57
【问题描述】:

AsyncStorage 错误:未定义访问令牌

如何定义 accesstoken 我使用 php 作为后端 api 并使用 accesstoken 进行注册以存储在 Asyncstorage 中

async storeToken(accesstoken) {
    try {
      await AsyncStorage.setItem(AccessToken, accesstoken);
    } catch (error) {
      console.log('AsyncStorage error: ' + error.message);
    }
}
  userRegister = () =>{
    const {username} = this.state;
    const {password} = this.state;

    if(username=="" || password==""){
      alert("Please fill out all fields");
    }
    // else if (password!=cpassword) {
    //     alert("Passwords do not match.");
    // }
    else{
    fetch('http://192.168.01.1/test/register.php', {
      method: 'post',
      header:{
        'Accept': 'application/json',
        'Content-type': 'application/json'
      },
      body:JSON.stringify({
        username: username,
        password: password,

      })
    })
    .then((response) => response.json())
      .then((responseData) =>{
      // how to define accesstoken
      //this.storeToken('accessToken',JSON.stringify(results));
     //if using in this  then same error
        this.storeToken('accesstoken',responseData.accesstoken)
        if(responseData == 'User Registered Successfully'){
          Alert.alert(
            'Success',
            'User Registered Successfully',
            [
             // {text:'ok', onPress: () =>  this.props.navigation.goBack() }
            ]
          );
          //this.props.navigation.goBack()
        }else{
          alert(responseJson);
        }
      })
      .catch((error)=>{
        console.log(error);
      });
  }
}

如何调用 accesstoken 在函数中定义。如何找出这个 如果使用 await 函数来访问令牌,则会显示错误 await is not an function

【问题讨论】:

  • 您将第一个变量作为硬代码字符串而不是访问令牌值传递。从此 this.storeToken(responseData.accesstoken) 替换您的 this.storeToken 函数

标签: react-native async-await asyncstorage


【解决方案1】:
    userRegister = () =>{
        const {username, password } = this.state;

        if(username=="" || password==""){
          alert("Please fill out all fields");
        }
        // else if (password!=cpassword) {
        //     alert("Passwords do not match.");
        // }
        else{
        fetch('http://192.168.01.1/test/register.php', {
          method: 'post',
          header:{
            'Accept': 'application/json',
            'Content-type': 'application/json'
          },
          body:JSON.stringify({
            username: username,
            password: password,

          })
        })
        .then((response) => {
          // console the response here, if it's containing accesstoken then pass
 it in your storeToken function like below
            this.storeToken(response.accesstoken)
            //after this do rest of your steps
          })
          .catch((error)=>{
            console.log(error);
          });
      }
    }

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 2013-07-11
    • 2019-05-06
    • 2019-11-16
    • 2012-07-18
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多