soccerway
soccerway

Reputation: 11991

While trying to get email id exists system throws exception during registration

While trying to check for unique email id, system throws exception. All I need is if email id entered exists in database, system should send a message to Register screen Player with same email address already exists !

I am getting the count for email found as 1, but below error occurred.

PUT http://localhost:8000/service/player 500 (Internal Server Error)
dispatchXhrRequest  @   xhr.js:178
xhrAdapter  @   xhr.js:12
dispatchRequest @   dispatchRequest.js:52
Promise.then (async)        
request @   Axios.js:61
Axios.<computed>    @   Axios.js:86
wrap    @   bind.js:9
fetchData   @   Register.js:56
onSubmit    @   Register.js:72

server.js

app.put('/service/player', async (req, res, next) => {
  try {
    const userEmail = req.body.email;
    const playerEmail = await UserModel.count({ where: { email: userEmail } }); 
    if(playerEmail==0){
      //If there is no email found, procced with normal registration here...
    const addPlayer = await UserModel.create(req.body);
    console.log("Server side PUT method log:" + addPlayer);
    res.status(200).json({ success: true });
    }else{
      return res.status(500).json({ message: "Email address already exists !"});
    }
  } catch (err) {
    return next(err);
  }
});

Register.js

    const [helperText, setHelperText] = useState('');
    const onSubmit  = () => {
    const fetchData = async () => {
      try {
     const res = await axios.put('http://localhost:8000/service/player', formRegister);
        console.log("Front End success message:" + res.data.success);
        if (res.data.success) {
          setIsSent(true);
          history.push('/login')
        }
        else {
          setHelperText(res.data.message);
        }
      } catch (e) {
        setHelperText(e.response.data.message);
      }
    }
    fetchData();
  }


<span className="registerValidationText">{helperText}</span>

Upvotes: 1

Views: 350

Answers (1)

gdh
gdh

Reputation: 13692

You are trying to render an object, hence the issue. Your server is sending an object { message: "Player with same email address already exists"}. In the catch block, you are setting an object to the state helperText. Instead, set the actual message to the state.

Like this

setHelperText(res.response.message);

Upvotes: 1

Related Questions