错误:操作可能没有未定义的“类型”属性。您可能拼错了一个动作类型字符串常量

问题描述 投票:0回答:1

我正在尝试登录并注册反应。使用react-redux,thunk v3.1.0,但出现错误 我的 auth.types.js 文件包括

Auth.action.js 包括

auth.reducer.js 包括

store.js 有

[![enter image description here][4]][4]

login.jsx 包含 loginHandle

如何解决这个错误

javascript reactjs redux react-redux redux-thunk
1个回答
0
投票

这里的问题是代码正在尝试将

formData
对象作为操作负载进行分派。

const formData = {
  email: data.get("email"),
  password: data.get("password"),
  // <-- no type property!
};

...

loginAPI(dispatch(formData)); // <-- dispatch(formData)!

formData
是一个具有未定义
type
属性的对象,因此您会看到错误。

loginAPI
是一个动作创建者,定义为首先接收
formData
有效负载并返回异步动作,即 Thunk,它传递给商店的
dispatch
函数。

您应该将

loginAPI
操作分派到商店。

dispatch(loginAPI(formData));

如果您想在导航之前等待异步

loginAPI
操作完成,那么您可能需要
await
它正在解决。

const handleSubmit = async (event) => {
  ...

  try {
    await dispatch(loginAPI(formData));
    navigate("/");
  } catch(error) {
    console.log(error);
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.