【问题标题】:After execution action: Unhandled Rejection (Error): Actions may not have an undefined "type" property. Have you misspelled a constant?执行后操作:未处理的拒绝(错误):操作可能没有未定义的“类型”属性。你有没有拼错一个常数?
【发布时间】:2020-02-19 01:52:26
【问题描述】:

未处理的拒绝(错误):操作可能没有未定义的“类型”属性。你有没有拼错一个常数?

试图在 redux 中创建一个动作来删除我的项目。我了解操作必须具有已定义的类型,因此不确定为什么会出现此错误

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

./类型

export const DELETE_TASK = 'DELETE_TASK';

./动作

import {DELETE_TASK} from './types'

export const deleteTask = id => dispatch => { axios .delete(`/api/tasks/${id}`) .then(res => dispatch({ type: DELETE_TASK, payload: id }) ) .catch(err => dispatch({ type: console.log(err) })) }

./reducers

case DELETE_TASK: return { ...state, tasks: state.dates.filter(task => task.id !== action.payload) };

./组件

onDeleteTask = id => { this.props.deleteTask(id) }

<button onClick={this.onDeleteTask.bind(this, e.id)}>delete task</button>

我必须在刷新页面项被删除时出错后提及!操作有效,但我不明白为什么在此之前会发生此错误

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    这部分代码有错误

    .catch(err => 
      dispatch({
        type: console.log(err)
      }))
    

    console.log() 总是返回 undefined。所以你正在发送一个动作{type: undefined}。可能你需要这样的东西

    .catch(err => {
      console.log(err)
      dispatch({
        type: ERROR
      })
    })
    

    假设您已经定义了ERROR 常量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多