Skip to content
Advertisement

Cannot read property ‘reduce’ of undefined data from firebase

I am trying to git the totalBalance of but i face error Cannot read property ‘reduce’ of undefined and in the same time i can iterate on the clients of the component the code is the below

JavaScript

i thought the issue is clients is undefined but i don’t know the reson

Advertisement

Answer

This error is telling you that the clients object which you are selecting from redux is undefined. It might be that it starts out as undefined and then populates with the data asynchronously, so it would be undefined on the first render but fine afterwards. If it is continuing to stay undefined then there is an issue somewhere else in your code.

There are two easy ways to deal with data that might not exist yet.

  1. You can replace undefined with an empty array and render the rest of the component normally. You would have a list with no items and 0 balance.
JavaScript
  1. You can stop the rest of the component from rendering. Either render nothing or a render some loading screen.
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement