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

Time:07-20

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

CodePudding user response:

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))
    }
})
  • Related