Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda