Remchi/bookworm-react

when I try to save a book I get this error => TypeError: Cannot read property 'catch' of undefined

Opened this issue · 12 comments

Hello, when I try to save a book i get this error,

TypeError: Cannot read property 'catch' of undefined
BookForm._this.onSubmit
C:/code/bookworm/src/components/forms/BookForm.js:55
  52 | this.setState({ errors });
  53 | if (Object.keys(errors).length === 0) {
  54 |   this.setState({ loading: true });
 55 |   this.props
  56 |     .submit(this.state.data)
  57 |     .catch(err =>
  58 |       this.setState({ errors: err.response.data.errors, loading: false })

I run mongo db in cmd windows like this :
C:\Program Files\MongoDB\Server\3.4\bin\mongod --dbpath C:\code\bookworm-api --port 27017

I have the same error,Have you solved it?

Hi @Remchi, just to follow on from what these two are saying. I am really struggling to solve the issue.

When I log in or sign up I keep on having an issue with the .catch in the onSubmit function.
I initially was getting this error, like the others above:
image

But when I console.logged the err I get to the real issue which is I am not catching my API error, but a redux error:

Error: Given action "USER_LOGGED_IN", 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 (combineReducers.js:123)
    at computeNextEntry (<anonymous>:2:27469)
    at recomputeStates (<anonymous>:2:27769)
    at <anonymous>:2:31382
    at Object.dispatch (createStore.js:165)
    at dispatch (<anonymous>:2:31875)
    at index.js:14
    at dispatch (applyMiddleware.js:35)
    at users.js:5
    at <anonymous>

What looks to me is that redux doesn't like an action being "cancelled" and wants us to explicitly handle it return the previous state. So my .catch is picking up an error from redux, before it is catching the error I am waiting for from the api.

Any idea how I would solve this issue? I can't find any meaningful differences between our code that would stop this from happening.

@nickmask same problem here

Even if i rewrote the api to work with mysql, i have the same problem. I found no work around or solution yet. I can't find if it's a redux or a react problem.

OK solved.

Hi @nickmask. I didn't find what was the bug, but i rewrote using redux-saga instead of thunk. Thought you might wanna look. Cheers! https://github.com/JuarezLopesJr/saga_book

Thanks @JuarezLopesJr . I am more use to sagas anyway, so I think I'll make the switch as well. Cheers!

I had this same issue, but I was able to fix it by adding a 'return' to the LoginPage submit function

may be your forgot to export USER_LOGGED_IN on types.js file, so that is make type on user Logged In(user) has value undefined.

Hi @iamkratos, I am also facing same issue. Can you please send me the exact location where to add return statement...if possible the please share the code.

Hi @Remchi, just to follow on from what these two are saying. I am really struggling to solve the issue.

When I log in or sign up I keep on having an issue with the .catch in the onSubmit function.
I initially was getting this error, like the others above:
image

But when I console.logged the err I get to the real issue which is I am not catching my API error, but a redux error:

Error: Given action "USER_LOGGED_IN", 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 (combineReducers.js:123)
    at computeNextEntry (<anonymous>:2:27469)
    at recomputeStates (<anonymous>:2:27769)
    at <anonymous>:2:31382
    at Object.dispatch (createStore.js:165)
    at dispatch (<anonymous>:2:31875)
    at index.js:14
    at dispatch (applyMiddleware.js:35)
    at users.js:5
    at <anonymous>

What looks to me is that redux doesn't like an action being "cancelled" and wants us to explicitly handle it return the previous state. So my .catch is picking up an error from redux, before it is catching the error I am waiting for from the api.

Any idea how I would solve this issue? I can't find any meaningful differences between our code that would stop this from happening.

You might forget to send the status code from the server. Do check it.

Hi @Remchi, just to follow on from what these two are saying. I am really struggling to solve the issue.

When I log in or sign up I keep on having an issue with the .catch in the onSubmit function.
I initially was getting this error, like the others above:
image

But when I console.logged the err I get to the real issue which is I am not catching my API error, but a redux error:

Error: Given action "USER_LOGGED_IN", 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 (combineReducers.js:123)
    at computeNextEntry (<anonymous>:2:27469)
    at recomputeStates (<anonymous>:2:27769)
    at <anonymous>:2:31382
    at Object.dispatch (createStore.js:165)
    at dispatch (<anonymous>:2:31875)
    at index.js:14
    at dispatch (applyMiddleware.js:35)
    at users.js:5
    at <anonymous>

What looks to me is that redux doesn't like an action being "cancelled" and wants us to explicitly handle it return the previous state. So my .catch is picking up an error from redux, before it is catching the error I am waiting for from the api.

Any idea how I would solve this issue? I can't find any meaningful differences between our code that would stop this from happening.

Do compare your code with this -> https://github.com/shah77/User-authentication.

image
From the actual video, played around with the curly braces and fixed it, hope it helps.