亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從服務器發送 2 組不同的錯誤消息

從服務器發送 2 組不同的錯誤消息

慕標琳琳 2021-12-23 19:27:26
當用戶提交注冊表時,我有一個來自某些驗證的錯誤數組。但與表單驗證分開的是檢查電子郵件地址是否存在。(使用快速驗證器)這是映射錯誤消息并將其發送回客戶端的方式: 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 });      }    }然后我循環遍歷客戶端上的數組以向用戶顯示錯誤消息。  {Array.isArray(this.state.errors) && (    <div className="alert alert-danger">      {this.state.errors.map((error, index) => (        <div key={index}>          {error}        </div>      ))}    </div>  )}如果電子郵件地址已被使用,則會顯示除電子郵件地址之外的所有錯誤。錯誤消息僅顯示在控制臺中,而不顯示在警報中。這就是我發回該錯誤消息的方式:  const emailExists = await User.findOne({ email: req.body.email });  if (emailExists) return res.status(400).send("Email already exists");我認為我的問題是其他錯誤在一個數組中,而唯一的電子郵件不在一個數組中。
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

如果您的電子郵件驗證,您需要返回包含errors鍵值作為字符串/段數組的響應對象


const emailExists = await User.findOne({ email: req.body.email });

if (emailExists) return res.status(400).send({ errors: ["Email already exists"] });


查看完整回答
反對 回復 2021-12-23
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號