0

我有一个简单的注册表单,其中包含电子邮件、密码和管理员代码字段。我将这些数据传递给我的动作创建者,并且在完成注册之前我让它通过三个动作。在那里的某个地方,它只是停止了。它不返回任何错误。这是我的代码。

export const checkAdminCode = ({ email, password, adminCode }, history) => {
  return (dispatch) => {
    dispatch({ type: CHECK_ADMIN_CODE });

    firebase.database().ref('/env/adminCode').once('value', (snapshot) => {
      if (snapshot.val() === adminCode) {
        return registerUser(email, password, history, dispatch);
      }

      return loginUserFail(dispatch);
    });
  };
};

const registerUser = (email, password, history, dispatch) => {
  return (dispatch) => {
    dispatch({ type: REGISTER_USER });

    firebase.auth().createUserWithEmailAndPassword(email, password)
      .then((user) => loginUserSuccess(dispatch, history, user))
      .catch((err) => loginUserFail(dispatch, err));      
  };
};

const loginUserSuccess = (dispatch, history, user) => {
  history.push('/admin/dashboard');
  dispatch({
    type: LOGIN_USER_SUCCESS,
    paylaod: user
  });
};

需要注意的几点:

  • if (snapshot.val() === adminCode) {确实有效。
  • 在我添加管理代码之前,我让表单直接提交registerUser并成功了。
  • 我放置了一些console.logs 试图找出故障发生的确切位置。测试以下代码后:

    const registerUser = (email, password, history, dispatch) => {
      console.log('test1');
      return (dispatch) => {
        console.log('test2');
        dispatch({ type: REGISTER_USER });
    
        firebase.auth().createUserWithEmailAndPassword(email, password)
          .then((user) => loginUserSuccess(dispatch, history, user))
          .catch((err) => loginUserFail(dispatch, err));      
      };
    };
    

console.log('test1');工作,console.log('test2');没有工作。

再一次,它没有返回错误,所以我不知道如何描述正在发生的事情或要搜索的内容。

请帮忙。提前致谢。

4

1 回答 1

0

registerUserasync redux action,你需要用dispatch.

您不需要dispatch显式传递给调用函数。

它将由 redux 隐式传递。

export const checkAdminCode = ({ email, password, adminCode }, history) => {
      return (dispatch) => {
        dispatch({ type: CHECK_ADMIN_CODE });

        firebase.database().ref('/env/adminCode').once('value', (snapshot) => {
          if (snapshot.val() === adminCode) {

            dispatch (registerUser(email, password, history));
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

          }

          return loginUserFail(dispatch);
        });
      };
    };
于 2018-01-14T06:36:12.297 回答