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...?