Search code examples
node.jsreactjsexpressaxioshttprequest

Sending data from react to node by get request using axios library


for more than 3 hours I'm handling with an issue, what I'm trying to do is sending a get request from react to nodejs using axios library, I wanna pass some data into this request, as we know the get request don't have a body, so I've sent the data as query parameter like that

    // base URL
    const url = "http://localhost:8080/loginAsTeacher";
    if(loginAs === "loginTeacher"){
        axios.get(url,{ 
            params :{
                email: "abc123@gmail.com",
                password: "abc1234*"
            }
        })
        .then(res => console.log(res)) // this line return status:200 and data:null
        .catch(err => console.log(err.message))
    }

so this request success but the problem is the email and password are not passing to the backend

router.get("/loginAsTeacher", async (req,res)=>{
    // values coming from the client
    const loginEmail = req.params.email;
    const loginPassword = req.params.password;
    console.log(req.params); // this line return {} empty object
    // get data of that user by his/her mail
    const teacherData = await myModel.findOne({
        email: loginEmail
    }).exec()
    res.status(200).json({
        status: 200,
        data: teacherData
    })
}) 

the console.log above return an empty object, which means there's no parameters

Is this not the right solution ???

thanks for reading


Solution

  • To get your queries you need to get req.query instead of req.params

    Btw. it's dangerous to send sensitive data over get. It could be get logged in plaintext even over https