【问题标题】:Unhandled Rejection (TypeError): Cannot read property 'someProperty' of undefined未处理的拒绝(TypeError):无法读取未定义的属性“someProperty”
【发布时间】:2021-10-19 20:35:43
【问题描述】:

当我按下“Marcar”按钮时出现此错误:

我的“Marcar”按钮触发了这个重击:

import axios from "axios";
import { addNewNote, binnacleNoteReview, loadBinnacleNotes } from "../Actions/notes.actions";

export const markNoteReview = (role, binnacle, noteNumber) => async (dispatch, getState) => {
   const response = await axios.post("http://localhost:5000/markNoteAsReviewed", {
      role,
      binnacle,
      noteNumber,
   });
   if (response.data.error) {
      alert("Something went wrong");
   }
   if (!response.data.error) {
   }
   dispatch(binnacleNoteReview(noteNumber, role));
};

这个 thunk 触发这个动作并发送:

export const BINNACLE_NOTE_REVIEW = "BINNACLE_NOTE_REVIEW";
export const binnacleNoteReview = (noteNumber, role) => ({
  type: BINNACLE_NOTE_REVIEW,
  payload: {
    noteNumber,
    role,
  },
});

reducer 看起来像这样(这是我得到错误的地方):

案例中出现的错误:

case BINNACLE_NOTE_REVIEW:

行内:

return state.notes.binnacleNotes.map((note) => {

减速机:

import { LOAD_BINNACLE_NOTES, BINNACLE_NOTE_REVIEW, ADD_NEW_NOTE, RESET_BINNACLE_NOTES } from "../Actions/notes.actions";

export const notes = (state = [], action) => {
   const { type, payload } = action;
   switch (type) {
      case LOAD_BINNACLE_NOTES:
         const { binnacleNotes } = payload;
         return {
            ...state,
            binnacleNotes,
         };
      case ADD_NEW_NOTE:
         const { id, date, binnacle_note, responsible, attachments, constructor_review, super_review, dro_review, timestamp } = payload;
         return {
            binnacleNotes: [
               ...state.binnacleNotes,
               {
                  id,
                  date,
                  binnacle_note,
                  responsible,
                  attachments,
                  constructor_review,
                  super_review,
                  dro_review,
                  timestamp,
               },
            ],
         };

      case BINNACLE_NOTE_REVIEW:
         const { noteNumber, role } = payload;
         return state.notes.binnacleNotes.map((note) => {
            switch (role) {
               case "super":
                  return {
                     ...state,
                     binnacleNotes: state.notes.binnacleNotes.map((note) => (note.id === action.id ? { ...notes, super_review: 1 } : note)),
                  };

               case "dro":
                  if (note.id == noteNumber) {
                     return {
                        ...note,
                        binnacleNotes: state.notes.binnacleNotes.map((note) => (note.id === action.id ? { ...notes, dro_review: 1 } : note)),
                     };
                  } else {
                     return note;
                  }

               case "constructor":
                  if (note.id == noteNumber) {
                     return {
                        ...note,
                        binnacleNotes: state.notes.binnacleNotes.map((note) => (note.id === action.id ? { ...notes, constructor_review: 1 } : note)),
                     };
                  } else {
                     return note;
                  }

               default:
                  return state;
            }
         });
      case RESET_BINNACLE_NOTES:
         return (state = []);
      default:
         return state;
   }
};

我的商店是这样的:

知道发生了什么吗?

编辑:

在解决了之前的错误之后:现在我的减速器/存储出现了这种行为:

我的对象按照其存储属性和值的顺序进行了更改:

之前:

之后:

【问题讨论】:

    标签: reactjs redux reducers redux-reducers


    【解决方案1】:

    因为BINNACLE_NOTE_REVIEWnotes 减速器中使用。所以状态是notes 对象。不是父节点。

    所以改成state.binnacleNotes.map(...)

    【讨论】:

    • 非常感谢越南!它工作得很好。我将再次阅读有关 redux 的文档。
    • 其实,我的店里发生了一些奇怪的变化,我可以再问一个问题吗?
    • 是的。请显示类似这个问题的详细信息
    猜你喜欢
    • 2019-04-07
    • 2019-07-19
    • 2019-06-03
    • 2020-04-09
    • 2019-11-13
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2022-11-26
    相关资源
    最近更新 更多