0

I'm using react js with express as backend and mysql. When i'm trying to show the data from the database with the next query I notice in the console of the browser that res.json returns two arrays

This is the code to get the data

export const allVehiculos = async (req,res) => {
    try {
        
        var query= "SELECT `idVehiculos`, `placa`, `codip`, `descp`, `anio`, `clase`, `color`, `motor`, `serie`, `marca` " +
        "FROM vehiculos AS vehiculos " +
        "INNER JOIN marcas as marcas " +
        "on `Vehiculos`.`idMarca` =  `marcas`.`id`"
        const vehiculos = await db.query(query)
        res.json(vehiculos)

    } catch (error) {
        res.json({message: error.message})
    }
}

This is the code of the react component

const [vehiculos, setVehiculo] = useState([])
    useEffect( () => {
        getVehiculos();
    }, [])

    return(
        vehiculos.map((item) => {
            return(
                <tr key={item.idVehiculos} className="table-row row-item">
                    <td>{item.placa}</td>
                    <td>{item.codip}</td>
                    <td>{item.descp}</td>
                    <td>{item.anio}</td>
                    <td>{item.clase}</td>
                    <td>{item.color}</td>
                    <td>{item.motor}</td>
                    <td>{item.serie}</td>
                    <td>{item.marca}</td>
                </tr>
            )})
    )

Result of res.json

Info not showing

But, when I change

res.json(vehiculos) 

to

res.json(vehiculos[0]) 

Then i got just one array and the information shows correctly

Info showing correctly

I don't know why res.json returns two arrays and how to get only one array

Sorry for my bad english, I'm very new in this field.

Thanks.

0 Answers0