简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM