Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React undefined is not an object

Have popular errors when I see TypeError: undefined is not an object (evaluating 'posts.map') , but I don’t understand where is wrong, can you help me and say where is wrong?

  const [posts, setPosts] = useState([]);

        const options = {
            method: 'GET',
            headers: {
                'X-RapidAPI-Host': 'mdblist.p.rapidapi.com',
                'X-RapidAPI-Key': 'key'
            }
        };
        
        fetch('https://mdblist.p.rapidapi.com/?i=tt0012195', options)
            .then(response => response.json())
            .then(res => {
                setPosts(res.data);
            })
                    
   
    {
        posts.map(item => {
            <div>{item.title}</div>
        })
    }
    

Update

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe at this moment the data is not received, try

conditional rendering

Change your code like following:

 posts && posts.map(item => {
                <div>{item.title}</div>
            })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading