仅显示 fetch 中错误对象的消息 api javascript

show only the message of error object in fetch api javascript

我想在控制台中记录错误消息。一切正常,除了我不能只记录消息,例如,如果电子邮件已经存在,它应该记录“电子邮件存在”

const submitHandler = async(event) => {
    event.preventDefault();
    const enteredEmail = emailInputRef.current.value;
    const enteredPassword = passwordInputRef.current.value;
    try {
      if (isLogin) {
           //do something
      } 
      
      else {
        setstate("sending request")
        const response = await fetch(
          "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=xxx",
          {
            method: "POST",
            body: JSON.stringify({
              email: enteredEmail,
              password: enteredPassword,
              returnSecureToken: true,
            }),
            headers: {
              "Content-Type": "application/json",
            },
          }
          )
        if(response.ok){
          alert("User added successfully")  
        }
        else{
          const data = await response.json()
          console.log(data);
          throw new Error(data);
        }
        setstate("Create new account")
      }
      
    } catch (error) {
      console.log(error.message);
    }
  };

不是抛出数据错误,而是抛出字符串消息错误。 Ref

throw new Error(data.message); // if data look like { message: 'email exists' }