AxiosError: Request failed with status code 500 when sending data from client to database

I am trying to send data from my client-side application to the server using Axios, but I am receiving an error:
AxiosError {message: ‘Request failed with status code 500’, name: ‘AxiosError’, code: ‘ERR_BAD_RESPONSE’}

When I test the same endpoint using Postman, the data is successfully passed to the database without any issues. This suggests that the server and database are functioning correctly, but there might be an issue with the client-side request.

`import React, { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";

    const Signup = () => {
      const [formData, setFormData] = useState({
        fname: "",
        lname: "",
        email: "",
        password: "",
        userType: "customer",
      });
      const navigate=useNavigate()
    
      const handleChange = (e) => {
        setFormData({ ...formData, [e.target.name]: e.target.value });
      };
      const handleSubmit = async (e) => {
        e.preventDefault();
        try {
          const res = await axios.post(
            "http://localhost:5000/api/users/register",
            formData,
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          );
          console.log(res.data);
          setFormData({
            fname: "",
            lname: "",
            email: "",
            password: "",
            userType: "customer",
          });
          if(res.status === 201){
            navigate('/login');
          }
        } catch (error) {
          console.error(error);
        }
      };
      return (
        <div className="flex items-center justify-center min-h-screen bg-gray-100">
          <form
            className="w-full max-w-md bg-white p-8 shadow-md rounded-md"
            onSubmit={handleSubmit}
          >
            <h2 className="text-2xl font-bold text-center mb-6">Sign Up</h2>
    
            <div className="mb-4">
              <label
                htmlFor="fname"
                className="block text-gray-700 font-medium mb-1"
              >
                First Name
              </label>
              <input
                type="text"
                id="fname"
                name="fname"
                value={formData.fname}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <div className="mb-4">
              <label
                htmlFor="lname"
                className="block text-gray-700 font-medium mb-1"
              >
                Last Name
              </label>
              <input
                type="text"
                id="lname"
                name="lname"
                value={formData.lname}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <div className="mb-4">
              <label
                htmlFor="email"
                className="block text-gray-700 font-medium mb-1"
              >
                Email
              </label>
              <input
                type="email"
                id="email"
                name="email"
                value={formData.email}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <div className="mb-6">
              <label
                htmlFor="password"
                className="block text-gray-700 font-medium mb-1"
              >
                Password
              </label>
              <input
                type="password"
                id="password"
                name="password"
                value={formData.password}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <button
              type="submit"
              className="w-full bg-orange-500 text-white py-2 px-4 rounded-md font-bold hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-orange-500"
            >
              Sign Up
            </button>
          </form>
        </div>
      );
    };
    
    export default Signup;` `const mongoose = require("mongoose");
    
    const UserSchema = new mongoose.Schema(
      {
        fname: {
          type: String,
          required: true,
        },
        lname: {
          type: String,
          required: true,
        },
        email: {
          type: String,
          required: true,
        },
        password: {
          type: String,
          required: true,
        },
        userType: {
          type: String,
          enum: ["admin", "user"],
          default: "user",
        },
      },
      {
        timestamps: true,
      }
    );
    
    module.exports = mongoose.model("User", UserSchema);` `const express = require("express");
    const router = express.Router();
    const User = require("../../models/User");
    const bcrypt = require("bcrypt");
    const jwt = require("jsonwebtoken");
    const authenticateToken = require("../../middleware/auth");
    
    //register
    router.post("/register", async (req, res) => {
      try {
        const salt = await bcrypt.genSalt(10);
        const password = await bcrypt.hash(req.body.password, salt);
        const userobj = {
          fname: req.body.fname,
          lname: req.body.lname,
          email: req.body.email,
          password: password,
          userType: req?.body?.userType || "user",
        };
        const user = new User(userobj);
        await user.save();
        return res.status(201).json(user);
      } catch (error) {
        res.status(500).json({ message: "Something went wrong" });
      }
    });
    module.exports = router;` `require('dotenv').config()
    const express=require('express');
    const app=express();
    const bodyParser=require("body-parser");
    const cors=require("cors");
    const connectDB=require('./config/db');
    
    
    //parse requests
    app.use(bodyParser.json());
    app.use(express.json());
    app.use(cors());
    
    
    //db
    connectDB();
    
    //routes
    //users route
    app.use('/api/users', require('./routes/api/user'))
    
    //task route
    app.use('/api/task', require('./routes/api/task'))
    
    const port=5000;
    app.listen(port, () =>{
        console.log(`Server is running on port ${port}`)
    })`

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

AxiosError: Request failed with status code 500 when sending data from client to database

I am trying to send data from my client-side application to the server using Axios, but I am receiving an error:
AxiosError {message: ‘Request failed with status code 500’, name: ‘AxiosError’, code: ‘ERR_BAD_RESPONSE’}

When I test the same endpoint using Postman, the data is successfully passed to the database without any issues. This suggests that the server and database are functioning correctly, but there might be an issue with the client-side request.

`import React, { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";

    const Signup = () => {
      const [formData, setFormData] = useState({
        fname: "",
        lname: "",
        email: "",
        password: "",
        userType: "customer",
      });
      const navigate=useNavigate()
    
      const handleChange = (e) => {
        setFormData({ ...formData, [e.target.name]: e.target.value });
      };
      const handleSubmit = async (e) => {
        e.preventDefault();
        try {
          const res = await axios.post(
            "http://localhost:5000/api/users/register",
            formData,
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          );
          console.log(res.data);
          setFormData({
            fname: "",
            lname: "",
            email: "",
            password: "",
            userType: "customer",
          });
          if(res.status === 201){
            navigate('/login');
          }
        } catch (error) {
          console.error(error);
        }
      };
      return (
        <div className="flex items-center justify-center min-h-screen bg-gray-100">
          <form
            className="w-full max-w-md bg-white p-8 shadow-md rounded-md"
            onSubmit={handleSubmit}
          >
            <h2 className="text-2xl font-bold text-center mb-6">Sign Up</h2>
    
            <div className="mb-4">
              <label
                htmlFor="fname"
                className="block text-gray-700 font-medium mb-1"
              >
                First Name
              </label>
              <input
                type="text"
                id="fname"
                name="fname"
                value={formData.fname}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <div className="mb-4">
              <label
                htmlFor="lname"
                className="block text-gray-700 font-medium mb-1"
              >
                Last Name
              </label>
              <input
                type="text"
                id="lname"
                name="lname"
                value={formData.lname}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <div className="mb-4">
              <label
                htmlFor="email"
                className="block text-gray-700 font-medium mb-1"
              >
                Email
              </label>
              <input
                type="email"
                id="email"
                name="email"
                value={formData.email}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <div className="mb-6">
              <label
                htmlFor="password"
                className="block text-gray-700 font-medium mb-1"
              >
                Password
              </label>
              <input
                type="password"
                id="password"
                name="password"
                value={formData.password}
                onChange={handleChange}
                className="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500"
                required
              />
            </div>
    
            <button
              type="submit"
              className="w-full bg-orange-500 text-white py-2 px-4 rounded-md font-bold hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-orange-500"
            >
              Sign Up
            </button>
          </form>
        </div>
      );
    };
    
    export default Signup;` `const mongoose = require("mongoose");
    
    const UserSchema = new mongoose.Schema(
      {
        fname: {
          type: String,
          required: true,
        },
        lname: {
          type: String,
          required: true,
        },
        email: {
          type: String,
          required: true,
        },
        password: {
          type: String,
          required: true,
        },
        userType: {
          type: String,
          enum: ["admin", "user"],
          default: "user",
        },
      },
      {
        timestamps: true,
      }
    );
    
    module.exports = mongoose.model("User", UserSchema);` `const express = require("express");
    const router = express.Router();
    const User = require("../../models/User");
    const bcrypt = require("bcrypt");
    const jwt = require("jsonwebtoken");
    const authenticateToken = require("../../middleware/auth");
    
    //register
    router.post("/register", async (req, res) => {
      try {
        const salt = await bcrypt.genSalt(10);
        const password = await bcrypt.hash(req.body.password, salt);
        const userobj = {
          fname: req.body.fname,
          lname: req.body.lname,
          email: req.body.email,
          password: password,
          userType: req?.body?.userType || "user",
        };
        const user = new User(userobj);
        await user.save();
        return res.status(201).json(user);
      } catch (error) {
        res.status(500).json({ message: "Something went wrong" });
      }
    });
    module.exports = router;` `require('dotenv').config()
    const express=require('express');
    const app=express();
    const bodyParser=require("body-parser");
    const cors=require("cors");
    const connectDB=require('./config/db');
    
    
    //parse requests
    app.use(bodyParser.json());
    app.use(express.json());
    app.use(cors());
    
    
    //db
    connectDB();
    
    //routes
    //users route
    app.use('/api/users', require('./routes/api/user'))
    
    //task route
    app.use('/api/task', require('./routes/api/task'))
    
    const port=5000;
    app.listen(port, () =>{
        console.log(`Server is running on port ${port}`)
    })`

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật