how to add params in useEffect axios get request

Backend code :
routes.get('/delivery/hotel/:hotelname',(req,res)=>{
const hotelName = req.params.hotelname;
res.json({'name': hotelName})
} )
routes.get('/delivery/hotel/:hotelname',(req,res)=>{
const hotelName = req.params.hotelname;
res.json({'name': hotelName})
} )
In reactjs , i don't know how to make that url cuz the hotelname could be different I tried like this
useEffect(()=> {
async function fetchData(){
let res= await axios.get('http://localhost:1010/delivery/hotel/:hotelName')
console.log(res)
}
fetchData()
},[])
useEffect(()=> {
async function fetchData(){
let res= await axios.get('http://localhost:1010/delivery/hotel/:hotelName')
console.log(res)
}
fetchData()
},[])
So idk how to set params in react axios ```
32 Replies
glutonium
glutonium3mo ago
u just put the name there in the link
axios.get("https://......./hotel/custonHotelName")
axios.get("https://......./hotel/custonHotelName")
vic
vicOP3mo ago
Yea , this is first time I use react with backend
glutonium
glutonium3mo ago
it's not anything with react it's just normal routing
vic
vicOP3mo ago
<Routes> <Route path='/delivery/hotel/:hotelName' element={component} > So I use useParams to get hotelName
glutonium
glutonium3mo ago
http://localhost:1010/delivery/hotel/customHotelName Open this url in your browser make sure your server is online it should return u "customHotelName" ooh u r also using react router
vic
vicOP3mo ago
Yep
glutonium
glutonium3mo ago
u have to fetch the route idk react router but ik that u have to first get the route from the url like the hotel nsme then you'll need to add that to the link
vic
vicOP3mo ago
I use like this const hotelName={useParams} and pass on fetch url with hotel name
glutonium
glutonium3mo ago
is it working! ? console log hotelName
vic
vicOP3mo ago
Yes it works But is it good practice?
glutonium
glutonium3mo ago
i don't know react router so can't really tell but if hotelName is holding the hotel name properly then u should he able to just do
axios.get(`https://......./hotel/${hotelName}`)
axios.get(`https://......./hotel/${hotelName}`)
vic
vicOP3mo ago
This is what I did
glutonium
glutonium3mo ago
here u r directly setting :hotelName in the param
vic
vicOP3mo ago
Yes that what I done
glutonium
glutonium3mo ago
that's not gon work this and this r diff do this
vic
vicOP3mo ago
I use let res= await axios.get(https://localhost:1010/deliver/hotel/${hotelName})
glutonium
glutonium3mo ago
hmm... and it is not working?
vic
vicOP3mo ago
It works
glutonium
glutonium3mo ago
weird
vic
vicOP3mo ago
But this is not working
glutonium
glutonium3mo ago
here it says deliver
vic
vicOP3mo ago
Type mistake
glutonium
glutonium3mo ago
on the server u have delivery ooh ok make sure u don't have those in your code
vic
vicOP3mo ago
Ok
glutonium
glutonium3mo ago
in what sense? does it give any error?
vic
vicOP3mo ago
It takes as a url and send no response Not as a params
glutonium
glutonium3mo ago
try console logging in the server console log hotelName before res.json see if it is receiving a response and make sure u have the url full like here i have skipped the middle part with ..... and no typo
vic
vicOP3mo ago
hotelName from router is working but U r message of http://localhost:1010/delivery/hotel/customHotelName Open this url in your browser make sure your server is online it should return u "customHotelName" This is not working
glutonium
glutonium3mo ago
it is not returning anything at all?
vic
vicOP3mo ago
Sorry for late , yes its not return anything
glutonium
glutonium3mo ago
np can u try this
vic
vicOP3mo ago
That works Works only from the react router useParams
Want results from more Discord servers?
Add your server