【问题标题】:Getting response back from using redux-thunk and AsyncStorage从使用 redux-thunk 和 AsyncStorage 获取响应
【发布时间】:2017-10-04 13:10:47
【问题描述】:

我正在使用 react-native、redux-thunk、jwt 和 AsyncStorage 来验证和定位该项目中的用户。似乎请求从 axios 转到后端,但我没有在 chrome 控制台中看到返回的响应('in axios' 和 '?' 没有显示),我不知道我的代码可能出了什么问题。

在我的操作/userAuth.js中

import { AsyncStorage } from 'react-native';
import axios from 'axios';

export function updateUserLoc(username, lat, lng) {
  return function(dispatch) {
    AsyncStorage.getItem('token').then(function(token) {
      console.log('out');
      axios.put(`${ROOT_URL}/:${username}/location`, {location: [lat, lng], token})
      .then((response) => {
        console.log('in axios');
        // console.log('update user location', response);
        dispatch({
          type: USER_LOC_UPDATE
        });
        console.log('?');
      })
      .catch(err => { console.log('user location err', err); });
    })
    .catch(err => { console.log('Async Storage err', err)});
  }
}

以前有没有人遇到过这种问题,或者有人知道这段代码的问题是什么以及如何调试它?

将不胜感激任何建议或回答。

谢谢。

【问题讨论】:

    标签: react-native jwt axios redux-thunk asyncstorage


    【解决方案1】:

    我认为AsyncStorage.getItem 不会返回 Promise。根据文档,getItem 采用可选的第二个参数,即您的回调函数。或者,如果您支持 ES7,您可以使用 async/await

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2017-09-16
      • 2018-04-12
      • 2020-07-12
      • 2020-10-28
      • 1970-01-01
      • 2016-06-12
      相关资源
      最近更新 更多