【问题标题】:action object showing up as undefined in redux动作对象在 redux 中显示为未定义
【发布时间】:2017-11-02 01:01:49
【问题描述】:

我正在尝试使用 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
  }
};

有人遇到过类似的问题吗?我在 return 语句之前 console.log 在我的 createUser 函数中,它甚至没有到达那里,我认为这很奇怪。同样在我尝试运行之前定义了功能操作。真的不知道我做错了什么:(

【问题讨论】:

  • 你在哪里打电话给createUser
  • @JoshKelley 我有import createUser from '~/redux/modules/authentication' 而不是import { createUser } from '~/redux/modules/authentication';该死的......谢谢你的提示!!!

标签: javascript redux


【解决方案1】:

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

例子:

import { createUser } from 'yourModulePath'

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 2016-02-26
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    相关资源
    最近更新 更多