Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
React how can you pass a variable to a get request

I'm trying to query from a table where the teacherId is equal to the teacherId of the person that logs in but I can't pass that teacherId from the front-end to the back-end.

This is the back end

app.get("/api/get", async(req,res) => {
    const teacherId = req.body.teacherId
    connection.query(
        "SELECT class FROM homework WHERE teacherID = ?",
        [teacherId],
        (err, result) => {
            if (result){
                res.send({ message: result })
            } else{
                console.log(err)
            }
        }
    )
})

This is the front end

 useEffect(() => {
        Axios.get("http://localhost:1337/api/get", {
            teacherId: teacherId
        }).then((response) => {
            if(response){
                setDisplayHomework(response.data.message)
            } else{
                console.log("error")
            }
        })
    })
const teacherId = localStorage.getItem("teacherId")

I think the problem lies where it says teacherId: teacherId but I don't know why.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You need to use

Axios.get("http://localhost:1337/api/get", {
     params: { teacherId }
 });

and use req.query.teacherId to read it


If you see the Axios.get signature it is

axios.get(url[, config])

in contrast to

axios.post(url[, data[, config]])

which passes the data as the second argument.

That is because the body in GET requests is not used by the servers. Read HTTP GET with request body for more on this.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Usually you do not send a body with a get request. Put the teacher id into the url. Then this is called path variable.

app.use('/teacher/:teacherId', function(req, res, next) {
  console.log(req.params.teacherId);
  next();
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Get requests don't have request bodies unlike post requests, as such the Axios.get function shouldn't have a second parameter for the body. Instead pass your parameters as a url, like this:

useEffect(() => {
        Axios.get("http://localhost:1337/api/get?teacherId="+teacherId).then((response) => {
            if(response){
                setDisplayHomework(response.data.message)
            } else{
                console.log("error")
            }
        })
    })

Then in your backend code use req.params to access the get request url parameters:

app.get("/api/get", async(req,res) => {
    const teacherId = req.params.teacherId
    connection.query(
        "SELECT class FROM homework WHERE teacherID = ?",
        [teacherId],
        (err, result) => {
            if (result){
                res.send({ message: result })
            } else{
                console.log(err)
            }
        }
    )
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda