1

I'm making a request from the frontend of my project to the backend. The frontend is react, the backend is nodeJs. I'm making this request:

axios.get("http://localhost:5050/user/verify-user", {email:l.email, password:l.password})
            .then(res => console.log(res))

And this is the code of the node function that should handle it:

const verifyUser = (req, res) => {
    console.log(req.body)
    const email = req.body.email;
    const password = req.body.password;
    console.log(email, password)

    User.findOne({ email:email})
        .then(user => {
            if(!user){
                res.json('User not registred');
            }else if(user.password != password){
                res.json('Uncorrect password');
            }else{
                res.json('Verified');
            }
        })
        .catch(err => res.status(400).json("ERROR:"+err))
}

But the console log startement always return undefined, so the output of the request is always 'User not registered'. If I make the same request from Postman it works properly. I read some answers telling that could be a proble of the format. If I console log it says that it is an object. Anyway, here is the code of the server:

const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require('body-parser')

require('dotenv').config();

const app = express();
const port = process.env.port || 5050;

app.use(morgan('dev'));

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended:true }));

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {useNewUrlParser:true});

const connection = mongoose.connection;
connection.once('open', () => console.log('Connected'));

const fileRouter = require('./routes/fileRoutes');
const userRouter = require('./routes/userRoutes');
const groupRouter = require('./routes/groupRoutes');
const extensionRouter = require('./routes/extensionRoutes');
const chatGRouter = require('./routes/chatGRoutes');

app.use('/file', fileRouter);
app.use('/user', userRouter);
app.use('/group', groupRouter);
app.use('/ext', extensionRouter);
app.use('/chat', chatGRouter);

app.listen(port, () => {console.log(`Server listening on port ${port}`)});

I don't know why it does not work. If someone knows, please help me

Matteo Possamai
  • 455
  • 1
  • 10
  • 23
  • `verifyUser` make that a post request and it will probably work. – Erenn Nov 29 '21 at 13:42
  • Try `{ params: {email:l.email, password:l.password} }` – Vitaliy Rayets Nov 29 '21 at 13:42
  • a HTTP GET request cannot contain a body. Look for the 'data' parameter https://github.com/axios/axios#request-method-aliases – Carsten Nov 29 '21 at 13:42
  • 1
    You have to send the email and password as parameters while making a GET request or have to make verifyUser a POST request. Check out more at this link: https://stackoverflow.com/questions/54005520/sending-request-body-for-get-method-in-axios-throws-error – Mahan Vyakti Nov 29 '21 at 13:46

0 Answers0