Actually i wanted to show some successfull message after hitting signup button using axios post method so that i have used toastify module in react but only catch option is working then is not working ...i am stuck here i dont know what to do...so please help me to sort this... here is my code
import React from 'react';
import {useState} from 'react';
import axios from 'axios';
import {toast} from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
toast.configure();
function Regform(){
const [user,setInput]=useState({
ecode:"",email:"",phone:"",password:"",cpassword:""
})
function Handle(event){
const {name,value}=event.target;
setInput(prevInput=>{
return {
...prevInput,
[name]:value
}
})
}
function postData(event){
event.preventDefault();
const newNote={
ecode:user.ecode,
email:user.email,
phone:user.phone,
password:user.password,
cpassword:user.cpassword
}
axios.post('http://localhost:4000/create',newNote)
.then( response => {
toast('Successfull registration ');
})
.catch( error => {
toast('Invalid registration ') ;
})
}
return (
<>
<form method="POST">
<fieldset>
<legend><i>Registration Form</i></legend>
<input type="text" placeholder="employee code" name="ecode" value =
{user.ecode} onChange={Handle} autoComplete="off"/><br/>
<input type="email" placeholder="email" name="email" value ={user.email}
onChange={Handle} autoComplete="off"/><br/>
<input type="number" placeholder="phone" name="phone" value ={user.phone}
onChange={Handle} autoComplete="off" /><br/>
<input type="password" placeholder="password" name="password" value =
{user.password} onChange={Handle} autoComplete="off"/><br/>
<input type="password" placeholder="confirm password" name="cpassword" value
={user.cpassword} onChange={Handle} autoComplete="off" /><br/>
<button onClick={postData}>Register</button>
</fieldset>
</form>
</>
)
}
export default Regform;
and here is my backend code for the same and for database i have used cloud mongo db
const mongoose =require('mongoose');
const express=require('express');
const cors=require('cors');
const app=express();
app.use(cors());
app.use(express.json());
mongoose.connect("mongodb+srv://raushan:
<password>@cluster0.ofx36.mongodb.net/nodeDB",{
useNewUrlParser:true,
useUnifiedTopology:true,
useCreateIndex:true
}).then( () => {
console.log('Connected to database ')
})
.catch( (err) => {
console.error(`Error connecting to the database.
\n${err}`);
})
app.use("/",require("./routes/noder"));
app.listen(4000,()=>{
console.log('sever is running at a port no 4000')
})