title: How to show error on duplicate email in reactJs?
body: <p>I have a MERN project. I have a form data. i want to insert in my database. Data is successfully inserted but <strong>I want to check duplicate email from my database and show error on reactjs page(like react toastify)</strong></p>
  <p><strong>basically i want to show some error to user</strong></p>
  <p><em>so how to fetch email from backend and show some result in reactjs.</em></p>
  <p><strong>As there are some same questions but it would't help me</strong></p>
  <p><em>So please give me some suggestions how can i do it?</em></p>
  <p><strong>register.js</strong></p>
  <pre><code>const Register = () =&gt; {
    const [newUser, setNewUser] = useState({
      identity: &quot;&quot;,
      names: &quot;&quot;,
      phone: &quot;&quot;,
      email: &quot;&quot;,
      city: &quot;&quot;,
      address: &quot;&quot;,
      subject: &quot;&quot;,
      classes: &quot;&quot;,
      message: &quot;&quot;,
    });
  
    
  
    const handleSubmit = (e) =&gt; {
      e.preventDefault();
      axios({
        method: &quot;post&quot;,
        url: &quot;/register&quot;,
        data: JSON.stringify(newUser),
        headers: { &quot;Content-Type&quot;: &quot;application/json&quot; },
      }).then((response) =&gt; {
          toast.dark(`${newUser.names}  registered Sucessfully `, {
            position: &quot;top-center&quot;,
            autoClose: 5000,
            hideProgressBar: false,
            closeOnClick: true,
            pauseOnHover: true,
            draggable: true,
            progress: undefined,
            });
        })
        .then((data) =&gt; {
          console.log(data);
        })
        .catch((error) =&gt; {
          
          if (error.response) {
            console.log(error.response.data);
          }
        });
    };
  
    const handleChange = (e) =&gt; {
      setNewUser({ ...newUser, [e.target.name]: e.target.value });
    };
  
  </code></pre>
  <p>router.post(&quot;/register&quot;, async (req, res) =&gt; {
  const { identity, names, phone, email, city, address, subject, classes, message } = req.body</p>
  <pre><code>// if (!identity || !names || !phone || !email || !city || !address || !subject || !classes || !message) {
  //     return res.status(422).json({ error: &quot;Please mark all the field&quot; })
  // }
  
  try {
  
      const userExit = await User.findOne({ email: email })
      if (userExit) {
          // res.status(422).json({ error: &quot;Email already exit&quot; })
          alert(&quot;email already exit&quot;)
          
      }
      // else if (password != cpassword) {
      //     return res.status(422).json({ error: &quot;password and confirm password don not match&quot; })
      // }
     
          const user = new User({identity ,names,  phone,email,city,address, subject, classes, message })
          await user.save()
          res.status(201).json({ message: &quot;user registered successfulluy&quot; })
        
      
      
  } catch (err) {
      console.log(err)
  }
  </code></pre>
  <p>})</p>
  <p><strong>auth.js</strong></p>
  <pre><code>router.post(&quot;/register&quot;, async (req, res) =&gt; {
      const { identity, names, phone, email, city, address, subject, classes, message } = req.body
      
  
      try {
  
          const userExit = await User.findOne({ email: email })
          if (userExit) {
              // res.status(422).json({ error: &quot;Email already exit&quot; })
              alert(&quot;email already exit&quot;)
              
              const user = new User({identity ,names,  phone,email,city,address, subject, classes, message })
              await user.save()
              res.status(201).json({ message: &quot;user registered successfulluy&quot; })
            
          
          
      } catch (err) {
          console.log(err)
      }
  
  })
  </code></pre>