1

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);
        })
    });

2 Answers 2

1

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);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.