1

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>
soccerway
  • 10,371
  • 19
  • 67
  • 132

1 Answers1

1

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);
gdh
  • 13,114
  • 2
  • 16
  • 28
  • I have tried that too, but getting `Error: Request failed with status code 500 at createError (createError.js:16) ` and i cant see the my message in front end. – soccerway Jun 07 '20 at 05:42
  • ok - please share the output of `console.log(error)` .. .pls also show the code of render method..thanks – gdh Jun 07 '20 at 05:45
  • When email exists i am getting a console log as `PUT http://localhost:8000/service/player 500 (Internal Server Error)` is that normal ? I have update my question with recent code.. – soccerway Jun 07 '20 at 09:30
  • for email already exists better to [use status code 409](https://stackoverflow.com/questions/50946698/422-or-409-status-code-for-existing-email-during-signup) – gdh Jun 07 '20 at 09:43
  • also in the else part of axios, console.log `res.data` and `res.data.message` and just keep an eye on what exactly is coming from the server – gdh Jun 07 '20 at 09:45
  • If email exists it is going to `catch(e)` section and on success it will navigate to login screen. So in which scenario it can go to else part ? – soccerway Jun 07 '20 at 09:58