I have a react website that has a page (movie detail) from where I need to query mySQL tables in order to display content. I am using Express server as middlewhere to receive the calls, make DB query and send back result.
The problem is that at that same page (movie detail), I need to query 2 different DB tables, let’s say one from the product details at top of the page and the other for gallery images at the bottom. In my index.js Express backend
I make a query to get the movie info from my movie page this way:
useEffect(()=> {
const fetchMovies = async () => {
try{
const res = await axios.get("/api/movie/"+id)
setMovies(res.data)
} catch(err) {
console.log(err)
}
}
fetchMovies()
},[])
Then below at the same page I need to make another query for the gallery images like this:
useEffect(()=> {
const fetchPics = async () => {
try{
const res = await axios.get("/api/movie/"+id)
//console.log(id)
setPics(res.data)
} catch(err) {
console.log(err)
}
}
fetchPics()
},[])
And my question is how do I handle from the backend this two different requests from same endpoint, or in other words how will express know when to serve result from one or the other, given that both come from same endpoint?
I have this in my index.js server
app.get("/movie/:id", (req,res) => {
const q = "SELECT * FROM movies"
pool.query (q, (err, data) => {
if (err) return res.json(err)
return res.json(data)
})
})
But this only returns one query (movie info) and I still need to make the second query (gallery images).
Can someone tell me the bet way to achieve this? Thanks in advance!!