react-native - 如何将访问 token 从 API 存储到异步存储

标签 react-native async-await asyncstorage

AsyncStorage 错误:未定义访问 token

如何定义访问 token 我使用 php 作为后端 api 并使用访问 token 进行注册以存储在 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);
      });
  }
}

如何调用访问 token 在函数中定义。如何弄清楚这一点
如果使用 await 函数访问 token ,则会显示错误 await is not an function

最佳答案

    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);
          });
      }
    }

关于react-native - 如何将访问 token 从 API 存储到异步存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53910390/

相关文章:

React-native 同步存储解决方案

javascript - 如何在 React Native 中使用 require() 显示动态图像?

c# - 未等待嵌套异步 lambda

javascript - 使用 await async 获取数据而不使用 try catch

c# - 异步编程并在循环中等待

javascript - 在异步存储中保存和删除每周计划 react native

android - 修改 Expo Managed App 中的 Android gradle.properties

javascript - 如何将expo react-native自定义字体应用到整个容器

javascript - 如何在 native react 中禁用返回键?

javascript - 在 React Native 中使用 this.props.children 加载大组件是否存在性能问题?