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

162
Vistas
How to make an axios post to database?

I'm trying to make a login system which is part of my project with react, express, MySQL and Axios but I keep getting this error- Uncaught (in promise) Error: Request aborted

Server side:

const express = require("express");
const cors = require("cors");
const mysql = require("mysql");

const app = express();
app.use(express.json())
app.use(cors());

const db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "cool12345",
    database: "users"
})

db.connect(err => {
    if(err){
        return err;
    }
})

console.log(db)


app.post("/register", (req, res) => {
    const username = req.body.username;
    const password = req.body.password;
    const email = req.body.email;

    db.query("INSERT INTO teachers (name, email, password) VALUES (?,?)", [username, email, password], (error, result) => {
        console.log(error)
    })
 
    db.query("INSERT INTO teachers (name, email, password) VALUES ('test', 'test2', 'test3')")
})
 

app.listen(4000, () => {
    console.log("Listening on port 4000")
})

client side:

import React, { useState } from 'react'
import "../styling/SignUp.css";
import { useHistory } from "react-router-dom"
import Axios from "axios";



function SignUp() {
    const [usernameReg, setUsernameReg] = useState("")
    const [emailReg, setEmailReg] = useState("")
    const [passwordReg, setPasswordReg] = useState("")

    const register = () => {
        Axios.post("https://localhost:4000/register", {username: usernameReg, email:emailReg, password: passwordReg}).then((response) => {
            console.log(response)
        })
    }

the function in the client-side returns a form but there's too much code so I left it out of this question. The signup button has an onClick handler function which runs the register.

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

0

I doubt your localhost has an ssl certificate

try:

Axios.post("http://localhost:4000/register", {username: usernameReg, email:emailReg, password: passwordReg}).then((response) => {
            console.log(response)
})

http instead of https

EDIT: I was too hasty to jump to a conclusion. What is that object you are passing to axios? I think it should be:

Axios.post("http://localhost:4000/register", {
    data: {
        username: usernameReg, 
        email:emailReg, 
        password: passwordReg
    }
}).then((response) => {
        console.log(response)
}).catch(err => console.log(err))

You can also catch the error. (So it won't be 'uncaught')

about 4 years ago · Juan Pablo Isaza Denunciar

0

Axios.post("http://localhost:4000/register", headers: {
    'Content-Type': 'application/json',
   }, {
 data: {
    username: usernameReg, 
    email:emailReg, 
    password: passwordReg
 }
}).then((response) => {
    console.log(response)
}).catch(err => console.log(err))

Try setting a header called Content-Type/application/json when you are using a post request. application/json is used to transfer json data through requests.

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