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.
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')
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.