Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
How to get a variable from Frontend to the Backend using get API in reactjs with Express API

I want to get information from my freight Shipment table in order to process other information I need to do in the frontend. But I don't know how to grab the email of the logged-in user using Axios.get() method to use it to query my MySQL DB.

in my frontend, I defined a useState of loggedEmail and I am setting it to the currently logged-in user. how can I pass that to my backend using the GET method?

here is my code:

server:

app.get('/api/freightID', (req, res) => {

    const email = req.body. //how would i get the email from the front end to use it in my query?
    db.query("SELECT * FROM freight_shipment WHERE user_email = ?", email, (err, result) => {
        if(err){
            console.log(err)
        }
        if(result.length > 0 ){
            res.send(result);
        }
    });
});

Frontend:

const [loggedEmail,setLoggedEmail] = useState("");
    Axios.defaults.withCredentials = true;
    useEffect(() => {
        Axios.get('http://localhost:3001/login').then((response) => {

            if(response.data.loggedIn == true){
                setLoggedEmail(response.data.user[0].email)
                setLoginStatus(response.data.loggedIn);
                console.log(response.data.loggedIn)
            }
        })
    },[]);

useEffect(() => {
        Axios.get("http://localhost:3001/api/freightID").then((response) => {
            console.log(response);
        })
    });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There is a config object in Axios.get where you can put your params there to send them to BE

You would use it like so:

Axios.get("http://localhost:3001/api/freightID", {
  params: { email: loggedEmail },  //<-- Put params here
}).then((response) => {
  console.log(response);
});

Then in your BE, you would get it like so:

app.get('/api/freightID', (req, res) => {

    const email = req.query.email //<-- It's here in the req.query
    db.query("SELECT * FROM freight_shipment WHERE user_email = ?", email, (err, result) => {
        if(err){
            console.log(err)
        }
        if(result.length > 0 ){
            res.send(result);
        }
    });
});
about 4 years ago · Juan Pablo Isaza Report

0

You can pass the loggedEmailin Get as a query param or path param.

Please take a look at this article. https://masteringjs.io/tutorials/axios/get-query-params.

In your case you can do like:

const params = {
  loggedInUserEmail: loggedEmailin 
};
Axios.get(`http://localhost:3001/api/freightID/{params.loggedInUserEmail}`)

and in Server side you can get value

app.get('/api/freightID/:email', (req, res) => {}) etc ... ///

var end = req.params['email']
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!