基于其他回调函数的功能



当用户想发布些什么时,他必须在其中唱歌(没有用户名& pass)。问题是我正在尝试在createpost()调用它时会致电singuser(),并且基于singuser()提取请求,它将再次调用createpost(),以便在用户登录后张贴。

这在crevepost组件中

CreatePost(){
    fetch(url ,{
      method :'POST',
      headers:{
        Accept:'application/json',
        'Content-Type' :'application/json',
      },
      body: JSON.stringify(post)
   }).then((response) => response.json())
  .then((responseJson)=>{
      if(responseJson.status =='inactive'){
               //SignUser
      }else{
             //post
      }
  }).catch((error)=>{ //later
  });
}

这是其他文件中的Singuser()

async function SignUser() {
           try{
          User.vtoken = await AsyncStorage.getItem('vtoken');
          var userTemp={
            vtoken: User.vtoken,
            ntoken : User.ntoken
            }
                fetch(url,{
                    method :'POST',
                    headers:{
                      Accep : 'application/json',
                      'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(userTemp)
                  }).then((response)=> response.json()).
                then((responseJson)=>{
                        if(responseJson.path == 2){
                            Save(responseJson, userTemp);}
                            else return;
                  }).catch((error)=>{
                     });
            
       }catch(error){}
}
async function Save(result , userTemp){
    try{
        await AsyncStorage.setItem('vtoken', result.vtoken);
            User.vtoken = result.vtoken;
            userTemp.vtoken = result.vtoken;
          fetch(url,{
            method :'POST',
            headers:{
              Accep : 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(userTemp)
          }).then((response)=>response.json()).
          then((responseJson)=>{
              return 'done';
          }).catch((error)=>{})
      }
      catch(error){}
}
export {SignUser}

我希望你明白如果有更好的方法可以做什么:

您可以做这样的事情:

const errorCodeMap = {
  USER_INACTIVE: 10,
}
const statusMap = {
  INACTIVE: `inactive`
}
const METHOD = `POST`
const APPLICATION_JSON = `application/json`
const headerDefault = {
  Accept: APPLICATION_JSON,
  'Content-Type': APPLICATION_JSON,
}
const who = `post`
async function createPost(payload, options) {
  try {
    const {
      url = ``,
      fetchOptions = {
        method: METHOD,
        headers: headerDefault,
      },
    } = options
    const {
      post,
    } = payload
    const response = await fetch(url, {
      ...fetchOptions,
      body: JSON.stringify(post)
    })
    const {
      status,
      someUsefulData,
    } = await response.json()
    if (status === statusMap.INACTIVE) {
      return {
        data: null,
        errors: [{
          type: who,
          code: errorCodeMap.USER_INACTIVE,
          message: `User inactive`
        }]
      }
    } else {
      const data = someNormalizeFunction(someUsefulData)
      return {
        data,
        errors: [],
      }
    }
  } catch (err) {
  }
}
async function createPostRepeatOnInactive(payload, options) {
  try {
    const {
      repeat = 1,
    } = options
    let index = repeat
    while (index--) {
      const { data, errors } = createPost(payload, options)
      if (errors.length) {
        await signUser()
      } else {
        return {
          data,
          errors,
        }
      }
    }
  } catch (err) {
  }
}

解决它,我做了很少的调整

async CreatePost(){
   try{
     var response = await fetch(url ,{
      method :'POST',
      headers:{
        Accept:'application/json',
        'Content-Type' :'application/json',
      },
      body: JSON.stringify(post)});
      var responseJson = await response.json();
      if(responseJson.status =='inactive' && postRepeat == true){
               postRepeat == false;
                await SignUser();
                this.CreatePost();
      }
       else{
        //posted
      }
}catch(err){}
}

相关内容

  • 没有找到相关文章

最新更新