當用戶提交注冊表時,我有一個來自某些驗證的錯誤數(shù)組。但與表單驗證分開的是檢查電子郵件地址是否存在。(使用快速驗證器)這是映射錯誤消息并將其發(fā)送回客戶端的方式: const errorMsg = errors.array().map(err => err.msg); return res.status(422).send(errorMsg);然后作為反應我有 try { const response = await register(user); console.log(response); } catch (ex) { if (ex.response) { const errors = ex.response.data; console.log(errors); this.setState({ errors }); } }然后我循環(huán)遍歷客戶端上的數(shù)組以向用戶顯示錯誤消息。 {Array.isArray(this.state.errors) && ( <div className="alert alert-danger"> {this.state.errors.map((error, index) => ( <div key={index}> {error} </div> ))} </div> )}如果電子郵件地址已被使用,則會顯示除電子郵件地址之外的所有錯誤。錯誤消息僅顯示在控制臺中,而不顯示在警報中。這就是我發(fā)回該錯誤消息的方式: const emailExists = await User.findOne({ email: req.body.email }); if (emailExists) return res.status(400).send("Email already exists");我認為我的問題是其他錯誤在一個數(shù)組中,而唯一的電子郵件不在一個數(shù)組中。
從服務器發(fā)送 2 組不同的錯誤消息
慕標琳琳
2021-12-23 19:27:26