Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
(express, react) How to fetch data from a foreign key via mySQL?

For context:

  • I have to make a friend request system. I got users signed up, and a page that shows all users within a list, each in a block with their names, and an "add" button. Once the button gets clicked, it sends in my DB into a [request] table the sender's user id, and the id of the receiver, each respectively into a table with "sender_ID(bigInt), receiver_ID(bigInt) and comfirmed(bool)" as the structure.

  • I want, on a friend list page, show every users that are my friends, and show as well the ones with a pending request either from me or from them.

  • In this friend list, I want to show those users the same as before, in a small block with their full name and username. Although that requires me to access their info via their ID (primary key, being in the users table. Either being a receiver or sender of a friend request).

Problem: I have no idea what code I should write to access a user's information via a foreign key. Currently using express and React together, and mySQL package for DB handling.

Fetch the requests from the table (request)

app.get("/api/friends", (req, res) => {
  const sqlSelect = "SELECT * FROM request"
  db.query(sqlSelect, (err, result) => {
      res.send(result)
  })
})

React friend list page

[...]

    const [friends, setFriends] = useState([])
    const getFriends = () => {
        Axios.get("http://localhost:3001/api/friends")
        .then((res)=> {
            setFriends(res.data)
            console.log(res.data)
        })
    }
    useEffect(() => {
        getFriends();
    }, []);


    return ( 
        <LoginContext.Provider value={{ loggedIn, setLoggedIn }}>
        <div>
            {friends.map((friend, key) => (
                <>
                <a key={friend.sender_id}>
                    <p>{friend.sender_id}</p>
                    <p>{friend.receiver_id}</p>
                </a>
                </>
            ))}
            
        </div>
        </LoginContext.Provider>
     );
}
 
export default Friendlist;

As you can see in the react code, in the render part, I have no trouble mapping what's coming from my [request] table, but that is not what I want. I want to be also able to fetch a user's information and display them, by using only THEIR id, so I don't end up showing every single users that's registered in the system. I don't know if y'all understand what I'm trying to do...?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda