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