Nested objects become empty after deep cloning

732

You are checking for undefined as a string "undefined" instead of:

if (action.payload.gambitStats.allTime !== undefined) { ...

or just:

if (!!action.payload.gambitStats.allTime)
Share:
732

Related videos on Youtube

SarKurd
Author by

SarKurd

Updated on December 07, 2022

Comments

  • SarKurd
    SarKurd over 1 year

    I have an object that trying too deeply clone it before mutate in Redux but the nested objects become empty after deep cloning it with lodash or json

    const initial = {
       infamy: {a: 1}
    }
    
    export const playerReducer = (state = initial, action) => {
      switch (action.type) {
        case SET_DATA:
          console.log("III", state);
          state = cloneDeep(state); //Why the neseted obj becomes empty?
          console.log("JJJ", state);
          break;
      }
    };

    Edit: looks look the issue was the condition i had for checking if the object was empty wasn't working so the empty data from the api was replacing the initial values but im wounding why the console.log was showing the post made mutation rather than pre made mutation

    case SET_DATA:
          console.log("III", state);
          const nextState = cloneDeep(state);
          console.log("JJJ", nextState); //why the log shows the change in line 10 made? shouldn't it log the values then change happen?
          nextState.membershipId = action.payload.membershipId;
          nextState.membershipType = action.payload.membershipType;
          nextState.displayName = action.payload.displayName;
          console.log(action.payload.gambitStats);
          if (action.payload.gambitStats.allTime !== "undefined") { //the bug is here
            nextState.gambitStats = cloneDeep(action.payload.gambitStats);
            nextState.infamy = cloneDeep(action.payload.infamy);
          }
          return nextState;

    • Justinas
      Justinas over 5 years
      Your code does not work on SO. Please provide working example.
    • Akrion
      Akrion over 5 years
      console.log('cloned', _.cloneDeep({infamy: {a: 1}})); works just fine. More details would be needed in your question.
    • SarKurd
      SarKurd over 5 years
      @Justinas i can't post the full code, stackoverflow doesn't allow but here a pastebin pastebin.com/ub5RiuwP
    • Justinas
      Justinas over 5 years
      So don't post full code, just example that shows your issue!
    • SarKurd
      SarKurd over 5 years
      @Akrion i can't do it this way, there will be more stuff in the object, it gets too complicated this way, i want to copy everything deeply check the pastebin above for the full file code or the repo here (the code is in playerReducer.js) github.com/SarKurd/Destiny-Tracker
    • SarKurd
      SarKurd over 5 years
      @Justinasi don't know how to post it more clearly, it works outside the reducer but inside not
  • SarKurd
    SarKurd over 5 years
    Thanks for the reply, i tried JSON stringify and parse too but it seems the problem is with my code, i can't figure out what causes the issue. I did exactly like u said but still empty
  • SarKurd
    SarKurd over 5 years
    It's been hours i'm checking every part of my code, the only place i manipulate the state is inside the Reducer and i'm calling the action to set data in 2 places only. It driving me crazy, i don't know what i'm doing wrong. I haven't written so much code, you can clone the repo if you want, i'll send you an API key
  • SarKurd
    SarKurd over 5 years
    I just noticed the only time the issue occurs is when the API return an empty object when it can't find the requested info but i'm not setting the payload when the response is empty rather just return the initial state
  • SarKurd
    SarKurd over 5 years
    Found it but still don't understand why that was causing an issue. the if statement for checking the object if empty wasn't working as intended but i'm logging before mutating SO why the log shows post mutation??? check the edited post