使用Redux工具包创建操作;传递有效负载

3lxsmp7m  于 2023-03-12  发布在  其他
关注(0)|答案(1)|浏览(163)

我目前正在开发我的应用程序的登录组件部分。它要求用户使用他们的电子邮件和密码登录。它在后端集成了firebase。我一直在使用redux工具包开发大部分应用程序。然而,我在createAction和尝试成功传递有效负载时遇到了问题。我还想指出的是,我是通过sagas进行异步操作的。有人能告诉我我做错了什么吗?我在控制台日志中提供了错误:

export const emailSignInStart = createAction(
  "user/EmailSignInStart",
  ({ email, password }) => ({
    payload: {
      email,
      password
    }
  })
);
export function* getSnapShotFromUserAuth(userAuth, additionalDetails) {
  try {
    const userSnapshot = yield call(
      createUserDocumentFromAuth,
      userAuth,
      additionalDetails
    );
    yield put(signInSuccess({ id: userSnapshot.id, ...userSnapshot.data() }));
  } catch (error) {
    yield put(signInFailure(error));
  }
}
export function* EmailSignIn({ payload: { email, password } }) {
  try {
    const { user } = yield call(
      signInAuthUserWithEmailAndPassword,
      email,
      password
    );
    yield call(getSnapShotFromUserAuth, user);
  } catch (error) {
    yield put(signInFailure(error));
  }
}

控制台中的结果:

payload
: 
TypeError: Cannot read properties of undefined (reading 'user') at eval (https://pyluef.csb.app/src/sagas/user/user.saga.js:67:29) at Generator.next (<anonymous>) at next (https://pyluef.csb.app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1161:27) at currCb (https://pyluef.csb.app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1255:7)
message
: 
"Cannot read properties of undefined (reading 'user')"
stack
: 
"TypeError: Cannot read properties of undefined (reading 'user')\n    at eval (https://pyluef.csb.app/src/sagas/user/user.saga.js:67:29)\n    at Generator.next (<anonymous>)\n    at next (https://pyluef.csb.app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1161:27)\n    at currCb (https://pyluef.csb.app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1255:7)"
[[Prototype]]
: 
Error
type
: 
"user/signInFailure"
@@redux-saga/SAGA_ACTION
: 
true

我使用的代码没有使用Redux工具包。它是这样工作的:

export const USER_ACTION_TYPES = {

  EMAIL_SIGN_IN_START: "user/EMAIL_SIGN_IN_START"
};
export const createAction = (type, payload) => ({ type, payload });
export const emailSignInStart = (email, password) =>
  createAction(USER_ACTION_TYPES.EMAIL_SIGN_IN_START, { email, password });
9bfwbjaz

9bfwbjaz1#

当你使用@redux/toolkit时,你不需要创建action -它会为你创建它们。从用户reducer文件导入action emailSignInStart,然后简单地把你的有效负载作为一个对象放在一个片段中,然后像这样调度action:

dispatch(emailSignInStart({email, password}));

相关问题