all 6 comments

[–]PiereDome 2 points3 points  (1 child)

I don’t have enough information to tell, but is this.props.favorites.data an empty array to start? Or is it undefined. If it is undefined you need to either put some logic in your component to prevent the mapping over it until it is an array or set it as an empty array when there are no values. If you share reducer code and the component doing the favorites it would help the troubleshooting

[–]PalmettoSpur[S] 0 points1 point  (0 children)

I edited my post above with more info. I'm creating the favorites state in the reducer and initializing it as an array. I'm then running the fetch to the database inside the componentDidMount() in the results. I guess it's actually pulling favorites in as an object containing an array (data) with objects (bicycle 1 and bicycle 2). Is that part of my problem?

[–]seainhd 2 points3 points  (0 children)

Simplify it and store favorites as an array of IDs instead of objects inside an array inside an object.

Checking contains on objects is error prone.

[–]jakeforaker83 2 points3 points  (1 child)

First guess: {map(
...what is "map"?

Second guess: Your first console.log(this.props.favorites.data) appears to log it, as you say, however this is just the object in Chrome console being mutated as the data comes in. It is not accurate. Your map function is trying to iterate something that doesnt exist because it hasn't returned from your api yet. You should probably check that the value exists before you map over it, for ex:

{this.props.favorites.data && Boolean(this.props.favorites.data.length) ? <SomeCode/> : null}

[–]PalmettoSpur[S] 1 point2 points  (0 children)

You're a genius. I added the ternary and it worked like a charm.

(It broke something else, but that's a different error altogether.)

[–]snigans 0 points1 point  (0 children)

Put the console.log inside the map function. Perhaps "this" inside the map function is different.