Getting could not insert data alert when button submitted… Why is it executing the catch part ?
import './App.css';
import { useState} from "react";
import Axios from 'axios';
function App() {
const [name, setName] = useState("");
const [age, setAge] = useState(0);
const addUser = () => {
Axios.post("http://localhost:3001/adduser", {
name: name,
age: age,
}).then(() => {
alert("data inserted successfully..");
}).catch(() => {
alert("could not insert data..");
});
};
return (
<div className="App">
<div className="inputs">
<input
type="text"
placeholder="Your name here..."
onChange={(event) => {
setName(event.target.value);
}}
/>
<input
type="number"
placeholder="Your age here..."
onChange={(event)=> {
setAge(event.target.value);
}}
/>
<button onClick={ addUser }>Add User</button>
</div>
</div>
);
}
export default App;