M
M
Max2021-04-22 16:21:37
React
Max, 2021-04-22 16:21:37

Why can't I iterate over an array using map. TypeError: test.map is not a function?

I get data from firebase via axios and I want to render it

The data comes to the render function as an array of objects
Here I put it in the console:
6081779bc6bcc105101938.png


render() {  
        const test = this.props.users;
        console.log('test', test);

        return (
            <div>
                 {
                    test.map(u => { 
                    return <div key={u.id}>
                             <div>{u.fullName}</div>
                           </div>
                        })
                }
            </div>
        )
    }


6081784a389df180557999.png

If the same data is output from the component, everything works
render() {
        const dataFromComponent = [  
                {id: 1, fullName: 'Max' },
                {id: 2,  fullName: 'Jordan' },
                {id: 3,  fullName: 'Gary' },
                {id: 4,  fullName: 'Oleg' } 
                ]

        return (
            <div>
                 {
                    dataFromComponent.map(u => { 
                    return <div key={u.id}>
                             <div>{u.fullName}</div>
                           </div>
                        })
                }
            </div>
        )
    }


On output:
if (!test.map ) {
            console.log("props", {...this.props})
        }

falls into the console:
6081a41fa91e6355168392.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-04-22
@Casufi

Are you sure that at some stage of the life cycle your test is not undefined?
https://reactjs.org/docs/state-and-lifecycle.html
https://reactjs.org/docs/conditional-rendering.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question