Alexandre Guindeuil
Alexandre Guindeuil

Reputation: 168

Redux reducer not showing my console log and returning undefined

Hi i'm having an issue with my Redux application since I added JWT to my nest API , here is the code of my reducer :

export default function reducer(state = {}, action) {
    switch (action.type) {
      case 'USER_LOGIN_SUCCESS':
        console.log('USER_LOGIN_SUCCESS')
        return action.payload.user;
      case 'USER_REGISTER_SUCCESS':
        // user exist
        if(action.payload.status === 409){
            console.log(action.payload)
            return state;
        }
        console.log(action.payload)
        return state
      case 'USER_REGISTER_FAILURE':
      case 'USER_LOGIN_FAILURE':
      default:
        console.log('lol');
        return state
    }
  }

When I login to my API with redux-api-middleware this is what I've got in my console :

redux.js:463 Uncaught (in promise) Error: Given action "USER_LOGIN_SUCCESS", reducer "user" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.
    at combination (redux.js:463)
    at dispatch (redux.js:213)
    at _callee$ (index.umd.js:2874)
    at s (index.js:1)
    at Generator._invoke (index.js:1)
    at Generator.e.<computed> [as next] (index.js:1)
    at asyncGeneratorStep (index.umd.js:33)
    at _next (index.umd.js:55)

When I tried my request with POSTMAN it worked perfectly :/ The thing is that it's not even showing the console.log in my reducer case : 'USER_LOGIN_SUCCESS'

Here is the request's response on POSTMAN : POSTMAN RESPONSE

Upvotes: 2

Views: 2983

Answers (1)

Jackson
Jackson

Reputation: 3518

In your USER_LOGIN_SUCCESS case you are returning the action payload value.

With a reducer you must alway return the state object or an immutable copy of the state object.

It is not clear what you reducer state object looks like but I presume it contains a user property

return {
  ...state,
  user: action.payload.user
}

Upvotes: 1

Related Questions