簡體   English   中英

未處理的拒絕(TypeError):無法讀取未定義的屬性“ id”

[英]Unhandled Rejection (TypeError): Cannot read property 'id' of undefined

我目前在我的react / redux項目中遇到一個未處理的拒絕錯誤,我對為什么會這樣有些困惑。 我一直在尋找答案,已經有好幾個小時沒有成功了。 在嘗試添加Firebase(ref.push)之前,此方法已成功運行。 如果需要,我可以發布工作版本代碼。 最初我只是使用db.json。 任何幫助,將不勝感激。 我完全感到頭疼,但是我知道這對於更有經驗的人來說將是顯而易見的。

創建流操作(錯誤在第2行引發)

  export const createStream = (formValues) => {
  return async (dispatch, getState) => {
    const { userId } = getState().auth;
    const response = await ref.push({ ...formValues, userId });
    console.log(response.data);
    dispatch({ type: CREATE_STREAM, payload: response.data});

    //Navigation to get user back to streams list
    history.push('/');
  };
};

減速機

export default (state = {}, action) => {
    switch (action.type){
      case FETCH_STREAMS:
        return {...state, ..._.mapKeys(action.payload, 'id')};
      case FETCH_STREAM:
        return {...state, [action.payload.id]: action.payload};
      case CREATE_STREAM:
        return {...state, [action.payload.id]: action.payload};
      case EDIT_STREAM:
        return {...state, [action.payload.id]: action.payload};
      /* case DELETE_STREAM:
        return _.omit(state, action.payload); */
      default: 
        return state;
    }
  }

eval
webpack-internal:///./src/reducers/streamReducer.js:24:326
combination
node_modules/babel-loader/lib/index.js??ref--6-oneOf-2!/Users/seth/projects/twitchclone/client/node_modules/redux/es/redux.js:454
p
<anonymous>:1:36402
v
<anonymous>:1:36684
(anonymous function)
<anonymous>:1:40069
dispatch
node_modules/babel-loader/lib/index.js??ref--6-oneOf-2!/Users/seth/projects/twitchclone/client/node_modules/redux/es/redux.js:213
e
<anonymous>:1:40553
eval
node_modules/babel-loader/lib/index.js??ref--6-oneOf-2!/Users/seth/projects/twitchclone/client/node_modules/redux-thunk/es/index.js:12
dispatch
node_modules/babel-loader/lib/index.js??ref--6-oneOf-2!/Users/seth/projects/twitchclone/client/node_modules/redux/es/redux.js:621

在動作創建者中,您似乎假設ref.push()返回的ref.push()解析為推入的值。 但是, 官方的API文檔不支持該理論。 它只是說:

返回 :非null firebase.database.ThenableReference 承諾與參考相結合; 在寫入完成時解決,但可以立即用作對子位置的引用。

因此,顯然,promise只是等待操作完成,而不產生任何值(即始終產生undefined )。

如您所知在將值保存到Firebase之前已經返回的值,您可以重組代碼以直接返回它。

// Store stream before passing it to ref.push()
const stream = { ...formValues, userId };
await ref.push(stream);

// Pass the stream as action payload
dispatch({ type: CREATE_STREAM, payload: stream });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM