在redux中显示为undefined的操作对象

时间:2017-06-01 03:34:14

标签: javascript redux

我尝试使用redux和redux thunk运行一些异步代码。无论出于何种原因当我尝试运行代码时,操作显示为未定义,我收到以下错误。

Cannot read property 'type' of undefined

我的所有动作创建者都会返回一个带有类型的对象,所以我不确定它的悲伤程度。

我像这样设置我的商店

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View
} from 'react-native';
import App from '~/components/App';
import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import * as reducers from './redux';
// import devTools from 'remote-redux-devtools';

const store = createStore(
  combineReducers(reducers),
  applyMiddleware(thunk)
)

const nimbus = () => {
  return (
    <Provider store={store}>
      <App />
    </Provider>
  );
}

我收到错误的我的authentication.js文件位于

之下
const AUTHENTICATING = 'AUTHENTICATING';
const NOT_AUTHED = 'NOT_AUTHED';
const IS_AUTHED = 'IS_AUTHED';

import { fbAuth, db } from '~/config/firebase';

function authenticating () {
  return {
    type: AUTHENTICATING
  }
}

function notAuthed () {
  return {
    type: NOT_AUTHED
  }
}

function isAuthed () {
  return {
    type: IS_AUTHED
  }
}

export function createUser (userData) {
  return function (dispatch, getState) {
    dispatch(authenticating());

    const email = userData.email;
    const password = userData.password;

    fbAuth.createUserWithEmailAndPassword(email, password).catch((error) => {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
    }).then(() => {
      const user = fbAuth.currentUser;

      db.ref('/users/' + user.uid).set({
        username: userData.username,
        displayName: userData.displayName,
        uid: user.uid
      })
      dispatch(isAuthed());
    }).catch((error) => {
      console.warn('Error in createUser callback', error)
    });
  }
}

const initialState = {
  isAuthed: false,
  isAuthenticating: false
};

export default function authentication (state = initialState, action) {
  console.log(action)
  switch (action.type) {
    case AUTHENTICATING :
      return {
        ...state,
        isAuthenticating: true
      }
    case NOT_AUTHED :
      return {
        isAuthenticating: false,
        isAuthed: false
      }
      case IS_AUTHED :
        return {
          isAuthenticating: false,
          isAuthed: true
        }
    default :
      return state
  }
};

有没有人遇到过类似的问题?我在返回语句之前的createUser函数中调用了console.log,它甚至没有到达那里,我认为这很奇怪。在我尝试运行之前,还定义了函数动作。真的不确定我做错了什么:(

1 个答案:

答案 0 :(得分:0)

在您的代码中,您需要为createUser()功能导入模块。

示例:

import { createUser } from 'yourModulePath'