Please find my code and error.
My App.js frontend code
import './App.css';
import { useState } from 'react';
import Axios from 'axios';
function App() {
const [Var_EName,setName] = useState("")
const [Var_Age,setAge] = useState(0)
const [Var_Country,setCountry] = useState("")
const [Var_Position,setPosition] = useState("")
const [Var_Sal,setSalary] = useState(0)
//const [employeeList, setEmployeeList] = useState([]);
const AddEmployee = () => {
Axios.post('http://192.168.137.1:3001/create',
{Ename:Var_EName,
EAge:Var_Age,
ECountry:Var_Country,
EPosition:Var_Position,
ESal:Var_Sal}).then(() =>
console.log("Succssfully Inserted")
)
}
const displayEmpInfo = () => {
console.log(Var_EName +Var_Age + Var_Country + Var_Position + Var_Sal)
};
return (
<div className="App">
<header className="App-header">CRUD Application Using NodeJS ReactApp MySql</header>
<div className="empinformation">
<label>Name :</label>
<input type="text" onChange={(event) => setName(event.target.value)} ></input>
<label>Age :</label>
<input type="number" onChange={(event) => setAge(event.target.value)}></input>
<label>Country :</label>
<input type="text" onChange={(event) => setCountry(event.target.value)}></input>
<label>Position :</label>
<input type="text" onChange={(event) => setPosition(event.target.value)}></input>
<label>Salary(Per Year) :</label>
<input type="number" onChange={(event) => setSalary(event.target.value)}></input>
<button onClick={AddEmployee}>Add Employee</button>
<button onClick={displayEmpInfo}>Disp Emp</button>
</div>
</div>
);
}
export default App;
And my index.js code for backend as below.
const express = require("express");
const app = express();
const mysql = require("mysql");
const cors = require("cors");
app.use(cors());
app.use(express.json());
const db = mysql.createConnection({
user: "root",
host:"localhost",
password:"Kalpataru",
database:"sakila"
});
db.connect(function (err) {
// connection.end();
if (!err) {
console.log("Database is connected ... \n");
} else {
console.log("Error connecting database ... \n");
}
});
app.post("/create",(req,res) => {
console.log('Employee Name : ' + req.body.EName);
const Var_EName = req.body.EName;
const Var_Age = req.body.EAge;
const Var_Country = req.body.ECountry;
const Var_Position = req.body.EPosition;
const Var_Sal = req.body.ESal;
db.query("INSERT INTO employes(Ename,EAge,ECountry,EPosition,ESal) VALUES(?,?,?,?,?)",
[Var_EName,Var_Age,Var_Country,Var_Position,Var_Sal],
(err,result) => {
if (err)
{
console.log(err)
}else
{
res.send("Values Inserted & Saved To Database.")
}
}
);
});
app.listen(3001,() => {
console.log("Yeah, Your Server is running on Port : 3001")
});
The below error i get in server terminal.
PS C:\CRUD-APPLICATION\server> node index.js
Yeah, Your Server is running on Port : 3001
Database is connected ...
Employee Name : undefined
Error: ER_BAD_NULL_ERROR: Column 'Ename' cannot be null
at Query.Sequence._packetToError (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (node:events:369:20)
at addChunk (node:internal/streams/readable:313:12)
--------------------
at Protocol._enqueue (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\CRUD-APPLICATION\server\node_modules\mysql\lib\Connection.js:198:25)
at C:\CRUD-APPLICATION\server\index.js:37:8
at Layer.handle [as handle_request] (C:\CRUD-APPLICATION\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\CRUD-APPLICATION\server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\CRUD-APPLICATION\server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\CRUD-APPLICATION\server\node_modules\express\lib\router\layer.js:95:5)
at C:\CRUD-APPLICATION\server\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\CRUD-APPLICATION\server\node_modules\express\lib\router\index.js:335:12)
at next (C:\CRUD-APPLICATION\server\node_modules\express\lib\router\index.js:275:10) {
code: 'ER_BAD_NULL_ERROR',
errno: 1048,
sqlMessage: "Column 'Ename' cannot be null",
sqlState: '23000',
index: 0,
**sql: "INSERT INTO employes(Ename,EAge,ECountry,EPosition,ESal) VALUES(NULL,'38','India','Developer','800000')"**
}
sql: "INSERT INTO employes(Ename,EAge,ECountry,EPosition,ESal) VALUES(NULL,'38','India','Developer','800000')"
As You all can see in INSERT statement other values i am getting correctly values except Ename value passing as null
Where is the issue & how to solve the issue ?
Please reply.