Home > database >  How do I use the params on my browser url on axios GET request
How do I use the params on my browser url on axios GET request

Time:12-17

Currently, I have been able to get the data into my component using

let {id} = useParams();

And when I call

{id} within my component, I get the id from the browser,s url. I however need it to enable me fetch data from my db

I have tried

const getData = () => {
     axios.get(`/API/joindiscussion/`   {id}, {
     //do something
     }):
}

But it didn't work

CodePudding user response:

nodejs api route /API/joindiscussion/:id

passed in react <Router path="/exmple/:id" element={} />

Example component used const id =useParams()

const getData = () => {

 axios.get(`/API/joindiscussion/${id}`, {
 //do something

 }):

}

OR

nodejs api route /API/joindiscussion?id=123

const getData = () => {

 axios.get(`/API/joindiscussion?${id}`, {
 //do something
 }):

}

this above concept in ES6 javascript

CodePudding user response:

Check your API and how it takes the id ( I'm assuming you're confused between the param and query method ).

If the API takes the id in param format, ( like : /API/joindiscussion/1234 where 1234 is the id) then your code should be

axios.get(`/API/joindiscussion/`   id,

If the API takes the id in query format ( like : /API/joindiscussion/?id=1234 where 1234 is the id ) then your code should be

axios.get(`/API/joindiscussion/?id=`   id,

comment if you have any doubts.

CodePudding user response:

Something like this:

let param = useParams()
  axios.get(`url/${param.id}`).then(response => {
    // do something with the response
  })
  • Related