OpenMeyends
OpenMeyends

Reputation: 129

Unhandled Rejection (Error): Actions may not have an undefined "type" property. Have you misspelled a constant?

trying to create an action in redux to call my API. I understand that actions must have a defined type so not sure why I am getting this error

Tried looking for similar posts but none of them provide the answer that I need

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
  })
);
}; 

EDIT: MaieonBrix helped me realize that my import of 'GET_TRACKS' was missing from the file where my action types are located:

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

file types.js:

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

Upvotes: 3

Views: 4669

Answers (1)

MaieonBrix
MaieonBrix

Reputation: 1624

This error occurs when you are dispatching an action with an undefined type like so :

dispatch({ type: variableThatMightBeUndefined })

It means that your GET_TRACKS variable that holds your 'GET_TRACKS' string is undefined.

I will update my answer if you update your post with how you are importing your action (it may be related to that)

UPDATE :

The issue here is how you exported your types

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

// into this

export const GET_TRACKS = ...

Here is the documentation on named export : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

Upvotes: 4

Related Questions