未处理的拒绝(错误):操作可能没有未定义的"type"属性。你拼错常量了吗?



试图在redux中创建一个操作来调用我的API。我知道动作必须有一个定义的类型,所以不确定为什么我会得到这个错误

尝试寻找类似的职位,但没有一个提供的答案,我需要

export const getTracks = () => dispatch => {
axios
.get("/api/tracks/all")
.then(res =>
dispatch({
type: GET_TRACKS,
payload: res.data
})
)
.catch(err =>
dispatch({
type: GET_TRACKS,
payload: null
})
);
}; 

编辑:MaieonBrix帮助我意识到,我的操作类型所在的文件中缺少"GET_TRACKS"的导入:

import { GET_ERRORS, GET_TRACKS } from "./types";

文件类型.js:

export const {GET_ERRORS} = 'GET_ERRORS';
export const {GET_TRACKS} = 'GET_TRACKS';

当您调度具有undefined类型的操作时,会发生此错误,如下所示:

dispatch({ type: variableThatMightBeUndefined })

这意味着保存"GET_TRACKS"字符串的GET_RACKS变量未定义。

如果你用你如何导入你的行动来更新你的帖子,我会更新我的答案(可能与此有关)

更新:

这里的问题是你如何导出你的类型

// change this
export const { GET_TRACKS } = ...
// into this
export const GET_TRACKS = ...

以下是有关命名导出的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

相关内容

最新更新