简体   繁体   English

未处理的拒绝(TypeError):无法读取未定义的属性“ id”

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

I'm currently experiencing an unhandled rejection error in my react/redux project and I'm a little confused as to why this is happening. 我目前在我的react / redux项目中遇到一个未处理的拒绝错误,我对为什么会这样有些困惑。 I've been looking around for answers now for hours now with no success. 我一直在寻找答案,已经有好几个小时没有成功了。 This was working successfully before I tried to add in firebase (ref.push). 在尝试添加Firebase(ref.push)之前,此方法已成功运行。 I can post the working version code if needed. 如果需要,我可以发布工作版本代码。 Originally I was just using db.json. 最初我只是使用db.json。 Any help would be appreciated. 任何帮助,将不胜感激。 I feel totally in over my head but I know this is gonna be something glaringly obvious to more experienced people. 我完全感到头疼,但是我知道这对于更有经验的人来说将是显而易见的。

create stream action (the error is throwing on line 2) 创建流操作(错误在第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('/');
  };
};

reducers 减速机

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

stack

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

In your action creator, you seem to assume that the promise returned by ref.push() resolves to the pushed value. 在动作创建者中,您似乎假设ref.push()返回的ref.push()解析为推入的值。 However, the official API documentaion doesn't support this theory. 但是, 官方的API文档不支持该理论。 It just says: 它只是说:

Returns : non-null firebase.database.ThenableReference . 返回 :非null firebase.database.ThenableReference Combined Promise and Reference; 承诺与参考相结合; resolves when write is complete, but can be used immediately as the Reference to the child location. 在写入完成时解决,但可以立即用作对子位置的引用。

So apparently, the promise is just for waiting for the operation to complete, and doesn't yield a value (ie, always yields undefined ). 因此,显然,promise只是等待操作完成,而不产生任何值(即始终产生undefined )。

As you know the value to return already before saving it to Firebase, you can restructure your code to return it directly. 如您所知在将值保存到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.

相关问题 未处理的拒绝(TypeError):无法读取未定义的 Stripe Javascript 的属性“id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Stripe Javascript ReactJS - 未处理的拒绝(TypeError):无法读取未定义的属性“id” - ReactJS - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined 无法读取未定义未处理拒绝的属性“_id” - Cannot read property '_id' of undefined unhandled Rejection (节点:19502)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):TypeError:无法读取未定义的属性&#39;indexOf&#39; - (node:19502) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'indexOf' of undefined 可能未处理 Promise 拒绝(id:0):类型错误:无法读取 react-native-track-player 中未定义的属性“标题” - Possible Unhandled Promise Rejection (id: 0): TypeError: Cannot read property 'title' of undefined in react-native-track-player 未处理的拒绝(TypeError):无法读取未定义的属性“子级” - Unhandled Rejection (TypeError): Cannot read property 'children' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“ city” - React- Unhandled Rejection (TypeError): Cannot read property 'city' of undefined ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined 反应未处理的拒绝(TypeError):无法读取未定义的属性“状态” - React Unhandled Rejection (TypeError): Cannot read property 'state' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM