0

I have a problem when using the information of a specific item from a JSON of an API.

When wanting to place the fields that I need, it throws the error:

TypeError: Cannot read property 'item' of undefined -.

This happenes when I want to put the field inside

return (
*value*
).

but if I put it out of the return and show it in console it works.

import React, { useState, useEffect } from 'react';

function Platillos_Detalle({ match }) {

    useEffect(() => {
        fetchItem();
    }, []);

    const [items, setItem] = useState({
        item: {}
    });

    const fetchItem = async () => {
        const fetchItem = await fetch(
            `https://fortnite-api.theapinetwork.com/item/get?id=${match.params.id
            }`
        );
        const items = await fetchItem.json();
        setItem(items);
        console.log(items.data.item.description)
    }

    return (
        <div className="container">
                <div>{items.data.item.description}</div>
    
        </div>


    )
}
export default Platillos_Detalle;

Result of console.log (items.data.item.description)

enter image description here

I'd also like to mention that this same code is used to do something similar but with several items and it works there fine.

update 1:

Focus on the "React Hooks useEffect" warning. As I use a parameter sent from another page (The id of the specific item) I had to define it in: []of useEffect. This solved the warning and now if it received the data from the JSON. Other problems were because of the JSON structure that looked like this:

JSON example

data: {
    itemID: "iditem",
    item: {
        name: "nombre",
        imagen: {
                inforamtion:"url"
        }
    }
}

so inside useState add the properties I needed (If I did not do this it would mark the error of "undefined item") and with that solve the error

function Platillos_Detalle({ match }) {

    useEffect(() => {
        fetchItem();
    }, [
        match.params.id
    ]);

    const fetchItem = async () => {
        const fetchItem = await fetch(
            `https://fortnite-api.theapinetwork.com/item/get?id=${match.params.id
            }`
        );
        const item = await fetchItem.json();
        setItem(item.data)
    };
    const [item, setItem] = useState({
        item: {
            images: {

            }
        }
    });
    return (
        <div className="center-block">
            <br />
            <div className="col-md-4 offset-md-4" >
                <div className="card">
                    <img src={item.item.images.information} className="card-img-top" alt="..." />
                    <div className="card-body">
                        <h5 className="card-title">{item.item.name} </h5>
                        <p className="card-text">{item.item.description}</p>
                    </div>
                    <div className="card-footer ">
                        <button className="btn btn-primary btn-lg btn-block">Pedir :v</button>
                    </div>
                </div>
                <br></br>
            </div>
        </div>
    )
}
export default Platillos_Detalle;

1 Answer 1

2

It’s probably not yet ready since its the result of the async code. Maybe try adding a condition checking if it exists.

Items.data ? Items .data.anything : []
Sign up to request clarification or add additional context in comments.

1 Comment

that helped me to see that I had to focus on the useEffects warning, and it is already correct. look at the update

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.