1

I'm trying to return an array from the reducer after filling it with data, but it always returns an empty array.

export default function dashboards(state: any = dashboardsState, action: any) {
 // more code
  if (action.type === Types.LOAD_DASHBOARD_SUCCESS) {
    // create the cards holder in the store
    var cardsSkull: any = [];
    Object.keys(action.payload.lists).map((idLis: any) => {
      cardsSkull[idLis] = {};
      console.log("still up");
    });
    action.payload.importedDashboards.map((impDash: any) => {
      Object.keys(impDash.mappedLists).map((key: any) => {
        const mpList = impDash.mappedLists[key];
        cardsSkull[mpList.idlistLocal][impDash.remote_board_id] = [1, 2, 3];
        console.log("still working");
      });
    });
    console.log(cardsSkull);
    // assign and ready to go
    return Object.assign({}, state, {
      selectedDashboardData: action.payload,
      cards: cardsSkull
    });
  }
// more code
return state;
}

when I console.log(cardsSkull) everything looks right.

enter image description here

I expect the cards to have the value of cardsSkull after it is filled, but the actual output is an empty array

9
  • try: cards: [...cardsSkull] Commented Apr 20, 2019 at 8:39
  • @F.bernal still the same result :/ Commented Apr 20, 2019 at 8:45
  • It's strange, can you post your client code? Commented Apr 20, 2019 at 8:52
  • client code ? just for information selectedDashboardData returned with value , only cards empty Commented Apr 20, 2019 at 9:06
  • Try doing console.log(JSON.stringify(cardsSkull)) and report what you get. Console.log kind of observe changes in arrays... Commented Apr 20, 2019 at 9:29

2 Answers 2

2

You define cardsSkull as array (which is an object under the hood), but because idLis is not a number, cardsSkull[idLis] = {}; does not populate an element within the array part of the object, but rather an element within the array object itself. Therefore in your screenshot, the length property of your array is still 0! Use numbers for idLis and your problem is solved.

Sign up to request clarification or add additional context in comments.

Comments

1

it seems that you are assuming wrong type for cards, it is an object instead of array. I would suggest to update as following

export default function dashboards(state: any = dashboardsState, action: any) {
  // more code
  if (action.type === Types.LOAD_DASHBOARD_SUCCESS) {
    // create the cards holder in the store
    const cardsSkull = Object.keys(action.payload.lists).reduce(
      (acc, idLis) => ({ ...acc, [idList]: {} }),
      {}
    );    

    action.payload.importedDashboards.forEach((impDash: any) => {
      Object.keys(impDash.mappedLists).forEach((key: any) => {
        const mpList = impDash.mappedLists[key];
        cardsSkull[mpList.idlistLocal][impDash.remote_board_id] = [1, 2, 3];
        console.log('still working');
      });
    });
    console.log(cardsSkull);
    // assign and ready to go
    return {
      ...state,
      selectedDashboardData: action.payload,
      cards: cardsSkull
    }
  }
  // more code
  return state;
}

1 Comment

it's working, thanks, man. tried a bunch of stuff without success and i forget to check my types again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.