reactjsreduxreducersredux-reducers

Unhandled Rejection (TypeError): Cannot read property 'someProperty' of undefined


I have this error when I press my "Marcar" Button:

enter image description here

My "Marcar" button fires this thunk:

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

And this thunk fires this action and dispatches:

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

And the reducer looks like this (here's where I got the error):

The error ocurrs in the case:

case BINNACLE_NOTE_REVIEW:

In the line:

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

Reducer:

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

My store looks like this:

enter image description here

Any idea of what's going on?

EDIT:

After resolved the previous error: now I have this behaviour with my reducer/store:

My object changed in the order its stores the properties and its values:

Before:

enter image description here

After:

enter image description here


Solution

  • Because BINNACLE_NOTE_REVIEW are using in the notes reducer. So state is notes object. Not the parent node.

    So just change to state.binnacleNotes.map(...)