尝试检查unique email
ID时,系统引发异常。我需要做的就是如果数据库中存在输入的电子邮件ID,系统应该向“注册”屏幕发送一条消息Player with same email address already exists !
我收到的电子邮件计数为1
,但发生以下错误。
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>
您正在尝试渲染对象,因此出现了问题。您的服务器正在发送对象{ message: "Player with same email address already exists"}
。在catch块中,您正在将对象设置为state helperText
。而是将实际消息设置为状态。
像这样
setHelperText(res.response.message);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句