如何将await关键字与asyncstorage setitem一起用于服务器响应?

时间:2018-10-25 05:22:37

标签: javascript react-native async-await asyncstorage

我正在尝试在我的React Native应用程序中使用asyncstorage。问题是我得到的服务器响应需要一些延迟,因此我想等待响应然后再使用该responseData.user_id保存到我的应用程序。我使用nodejs作为后端和mysql db。所以在用户注册后,我将它同时插入到db中,我编写了另一个查询以获取其user_id(PK)。因此此responseData正在获取到客户端我正在尝试从响应中获取该user_id。因此,我编写了这样的内容

   onPressRegister = async () => {

  try {
    let response = await fetch('http://192.168.1.2:3000/users/registration', {
      method: 'POST',
      headers: {
        'Accept': 'applictaion/json',
        'Content-Type': 'application/json',

      },
      body: JSON.stringify({
        contact: this.state.contact,
        password: this.state.password,
      })
    });

    let responseData = await response.json();

    if (responseData) {

      try {
                Action.firstScreen();
        await AsyncStorage.setItem('userid', JSON.stringify(responseData.userData.phone_no));
      }
      catch (e) {
        console.log('caught error', e);
      }

    }
  } catch (error) {
    console.error(error)
  }

}

在下一个屏幕中,我将像这样访问用户标识,并在下一个API调用中将其传递给它。

         getUserId = async () => {
      let userId = await AsyncStorage.getItem('userid');
      return userId;
    }


onPressYes = (workType) => {
        this.getUserId().then((userId) => {

   this.setState({userId:userId})

})

         fetch('http://192.168.1.2:3000/users/user_request',{
           method:'POST',
           headers:{
             'Accept': 'application/json',
             'Content-Type': 'application/json',
           },
           body:  JSON.stringify({
            workType,
            phone:this.state.userId
             })
         })
         .then(response => response.json())
         .then((responseData) => {
           this.setState({
           data:responseData
         });
         });
     }

但这是我得到的错误。 error

1 个答案:

答案 0 :(得分:1)

尝试一下:

 error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Deleted item is: ")) << Priority_Queue::displayRecord(tmp)'
    /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/x86_64-redhat-linux

要访问其他组件中的值,请执行以下操作:

onPressRegister = async () => {

  try {
    let response = await fetch('http://192.168.1.6:3000/users/registration', {
      method: 'POST',
      headers: {
        'Accept': 'applictaion/json',
        'Content-Type': 'application/json',

      },
      body: JSON.stringify({
        contact: this.state.contact,
        password: this.state.password,
      })
    });

    let responseData = await response.json();

    if (responseData) {

      try {
        await AsyncStorage.setItem('userid', JSON.stringify(responseData.user_id));
      }
      catch (e) {
        console.log('caught error', e);
      }

    }
  } catch (error) {
    console.error(error)
  }

}