React Native:可能未处理的Promise拒绝(id:0)

时间:2017-07-14 22:30:49

标签: javascript react-native redux react-redux redux-thunk

所以我试图通过React-Native了解有关Redux的更多信息。

我正在尝试向Axios发出HTTPS请求以从Web服务器提取数据。一切运行正常,我控制台记录动作,有效负载是我需要的数据。但它仍然会抛出'无法读取属性'数据'null'TypeError。

以下是我的代码:

import React, { Component } from 'react';
import ReduxThunk from 'redux-thunk';
import { StackNavigator } from 'react-navigation';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import reducers from '../reducers';
import ReduxTestObj from '../components/ReduxTestObj';

export default class ReduxTest extends Component {
    render() {
        return (
          <Provider store={createStore(reducers, {}, applyMiddleware(ReduxThunk))}>
            <ReduxTestObj />
          </Provider>
        );
      }
    }

这是ReduxTestObj

import React, { Component } from 'react';
import _ from 'lodash';
import { View } from 'react-native';
import { connect } from 'react-redux';
import DevBanner from './DevBanner';
import Header from './Header';
import { actionCreator } from '../actions';

class ReduxTestObj extends Component {
  componentWillMount() {
    this.props.actionCreator('urlIWantToGoTo');
  }
  getBannerText() {
    if (this.props.loading) {
      return 'PULLING DATA. GIVE ME A SECOND';
    }
    const rObj = _.map(this.state.data[1].recipient_list);
    const rName = [];
    for (let i = 0; i < rObj.length; i++) {
      rName[i] = rObj[i].team_key.substring(3);
    }
    const winnerMSG = `Teams ${rName[0]}, ${rName[1]}, ${rName[2]}, ${rName[3]} Won`;
    return winnerMSG;
  }
  render() {
    return (
      <View>
        <Header
          text={'Redux Testing'}
        />
        <DevBanner
         message={this.getBannerText()}
        />
      </View>
    );
  }
}

const mapStateToProps = state => {
  return {
    data: state.tba.data,
    loading: state.tba.loading
  };
};

export default connect(mapStateToProps, { actionCreator })(ReduxTestObj);

这是Action Creator

import axios from 'axios';
import { TBA_PULL } from './Types';


export const actionCreator = (url) => {
  return (dispatch) => {
    axios({
      method: 'get',
      url,
      responseType: 'json',
      headers: {
        'Auth-Key': 
'Hidden For Obvious Reasons'
      },
      baseURL: 'theBaseUrlOfTheWebsite'
    }).then(response => {
      dispatch({ type: TBA_PULL, payload: response.data });
    });
  };
};

这是Reducer

import { TBA_PULL } from '../actions/Types';

const INITIAL_STATE = { data: [], loading: true };
export default (state = INITIAL_STATE, action) => {
  console.log(action);
  switch (action.type) {
    case TBA_PULL:
      return { ...state, loading: false, data: action.payload };
    default:
      return state;
  }
};

如上所述,console.log(action)打印出来并且它拥有的数据是正确的。但它继续抛出以下错误: Error1

我尝试过改变一些东西,谷歌搜索,搜索,消除Action Reducer,尽可能通过返回一个字符串使其尽可能基本。它拒绝工作。 有没有人遇到类似的问题或知道如何解决它?

感谢您的时间。

编辑:我还在ReduxTestObj中控制记录'this'作为getBannerText()的第一行,它成功返回this.props.data作为我想要的数据,this.props.loading为false。但它仍然会引发同样的错误。

1 个答案:

答案 0 :(得分:0)

韦尔普。我想我只是犯了一个错误。

我在getBannerText()中调用This.state.data而不是this.props.data。

解决了这个问题。