Uncaught TypeError: Cannot read properties of undefined (reading 'then')?

306 Views Asked by At

saveProfile() does not return promise, although the person who has exactly the same code, the same function returns promise

  return async (dispatch, getState) => {
    const authUserId = getState().auth.id
    let data = await profileAPI.saveProfile(editDataAboutMe)
      if (data.resultCode === 0) {
      dispatch(setUserProfileThunkCreator(authUserId))
      return await Promise.resolve()
    } else {
      dispatch(stopSubmit('AboutMeEditForm', {_error: data.messages[0]}))
      return await Promise.reject()
    }
  }
}

mapDispatchToProps = (dispatch) => ({
      saveProfile: (editDataAboutMe) => {
        dispatch(saveProfileThunkCreator(editDataAboutMe))
     }
 }




const onSubmit = (editDataAboutMe) => {
    props.saveProfile(editDataAboutMe).then(() => {
      setEditMode(false)
    })
  }
1

There are 1 best solutions below

1
DustInComp On BEST ANSWER

Assuming dispatch returns the thunk's return value (or a Promise thereof), fix the saveProfile function to return it as well:

mapDispatchToProps = (dispatch) => ({
    saveProfile: (editDataAboutMe) => {
        return dispatch(saveProfileThunkCreator(editDataAboutMe))
    }
})