如何在React中将错误消息格式化为列表?

huangapple go评论71阅读模式
英文:

How to format error messages as list in React?

问题

const handleSubmit = (event) => {
  event.preventDefault();
  postWithoutAuth("/auth/signup", formValues)
    .then((response) => {
      setAlert("注册成功", "success");
    })
    .catch((error) => {
      error.response.data.errors.forEach(e => {
        setAlert(<li>{e.field + " " + e.message}</li>);
      })
    });
};
英文:

I return the following response from Spring Boot API and want to format properly on my React app:

Array(4)
0: {field: &#39;lastName&#39;, message: &#39;size must be between 3 and 50&#39;}
1: {field: &#39;username&#39;, message: &#39;size must be between 3 and 20&#39;}
2: {field: &#39;password&#39;, message: &#39;size must be between 6 and 100&#39;}
3: {field: &#39;firstName&#39;, message: &#39;size must be between 3 and 50&#39;}

I have the following alert method that displays messages. But I cannot format the message by including all the error messages by type:

const handleSubmit = (event) =&gt; {
  event.preventDefault();
  postWithoutAuth(&quot;/auth/signup&quot;, formValues)
    .then((response) =&gt; {
      setAlert(&quot;Signed up successfully&quot;, &quot;success&quot;);
    })
    .catch((error) =&gt; {
      error.response.data.errors.foreach(e =&gt; {
        setAlert(&lt;li&gt;{e.field + &quot; &quot; + e.message}&lt;/li&gt;);
      })
    });
};

Any idea?

答案1

得分: 0

1) 你在JS中所以`&lt;li&gt;{e.field + &quot; &quot; + e.message}&lt;/li&gt;` 需要变成 `&quot;&lt;li&gt;&quot; + e.field + &quot; &quot; + e.message + &quot;&lt;/li&gt;&quot;`
2) 你连续触发了很多警告
3) `forEach()`是区分大小写的你写成了`foreach()`
4) `setAlert()`中缺少了`severity`

假设`error.response.data.errors`包含你问题中的数组尝试以下代码

    .catch((error) =&gt; {
      var alertContents = '&#39;&lt;ul&gt;&#39;';
      error.response.data.errors.forEach(e =&gt; {
        alertContents += "&quot;&lt;li&gt;&quot; + e.field + &quot; &quot; + e.message + &quot;&lt;/li&gt;\n&quot;";
      });
        setAlert(alertContents + '&lt;/ul&gt;', 'error');   
    });
英文:

Four things

  1. you are in JS so &lt;li&gt;{e.field + &quot; &quot; + e.message}&lt;/li&gt; needs to be &quot;&lt;li&gt;&quot; + e.field + &quot; &quot; + e.message + &quot;&lt;/li&gt;&quot;
  2. you are pumping a lot of alerts in quick succession.
  3. forEach() is case sensitive. You have foreach()
  4. the severity on setAlert() is missing

Presuming that error.response.data.errors contains the array in your question, try the following:

.catch((error) =&gt; {
  var alertContents = &#39;&lt;ul&gt;&#39;;
  error.response.data.errors.forEach(e =&gt; {
    alertContents += &quot;&lt;li&gt;&quot; + e.field + &quot; &quot; + e.message + &quot;&lt;/li&gt;\n&quot;;
  });
    setAlert(alertContents + &lt;/ul&gt;, &quot;error&quot;);   
});

huangapple
  • 本文由 发表于 2023年3月12日 16:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定