Redux Reducer => 无法将 undefined 或 null 转换为对象

Redux Reducer => Cannot convert undefined or null to object

所以我有一个非常奇怪的错误,我不确定为什么或如何修复它。

Uncaught TypeError: Cannot convert undefined or null to object

所以这里有什么问题?我试图将默认状态直接添加到减速器中,我如何看到有关此错误的其他答案但对我不起作用。

操作:

export const createProfile = profileValues => async dispatch => {
  const response = await profileApi.post('/profileApi', profileValues);

  dispatch({ type: CREATE_PROFILE, payload: response.data });
};

减速器:

const initialState = {
    values: {}
  };
  
  export default function(state = initialState, action) {
    switch (action.type) {
      case CREATE_PROFILE:
        return { ...state, [action.payload.id]: action.payload };
      default:
        return state;
    }
  }

组件:

const Profile = (props) => {

    useEffect(() => (props.createProfile()))

    return (
        'Return'
    );
};

const mapStateToProps = state => {
    return { profile: Object.values(state.profile) };
  };
  
  export default connect(
    mapStateToProps,
    { createProfile }
  )(Profile);

Object.values() 无法将 nullundefined 转换为具有可提取属性的对象。

在 API returns 之前,mapStateToProps 选择器尝试获取 state.profile 的值,即 undefined:

const mapStateToProps = state => {
  return { profile: Object.values(state.profile) };
};

profile的初始值设为空对象:

const initialState = {
  values: {
    profile: {}
  }
};

或者在选择器级别使用后备值 - {}

return { profile: Object.values(state.profile ?? {}) };