Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我写得不正确,无法在警报/控制台中显示消息的原因是什么?_Javascript - Fatal编程技术网

Javascript 我写得不正确,无法在警报/控制台中显示消息的原因是什么?

Javascript 我写得不正确,无法在警报/控制台中显示消息的原因是什么?,javascript,Javascript,我在我的应用程序中注册。当我将表单发送到本地服务器时,此服务器中的用户名已注册,服务器返回错误400和响应(在我的浏览器/网络/请求文件/响应中,我看到): 我需要将此消息输出到警报或控制台。 我试着去做: const er = res[0].message; console.log('Error',er); 但控制台中没有显示任何内容。控制台中只有两条消息:POSThttp://localhost:3333/api/auth/register 400(错误请求)类型错误:无法读取未定义的属性

我在我的应用程序中注册。当我将表单发送到本地服务器时,此服务器中的用户名已注册,服务器返回错误400和响应(在我的浏览器/网络/请求文件/响应中,我看到):

我需要将此消息输出到警报或控制台。 我试着去做:

const er = res[0].message;
console.log('Error',er);
但控制台中没有显示任何内容。控制台中只有两条消息:
POSThttp://localhost:3333/api/auth/register 400(错误请求)类型错误:无法读取未定义的属性“消息”。

但必须仅显示此消息:
具有此用户名的用户已存在

我写这篇文章的代码(我评论在不同情况下发生的事情):

和函数api:

export const api = async (url, args) => { 
  const response = await fetch(`${apiUrl}${url}`, {
   ...args,
    headers: {
      "Content-type": "application/json; charset=UTF-8 " ,
      "Accept": 'application/json',
      ...args.headers,  
    },
  });

 return response.json();    

}

我可以在此代码中更改什么以显示警报或控制台消息:
具有此用户名的用户已存在


我可以在此代码中更改什么以显示警报或控制台此
错误消息?

请将onSubmit代码段修改为以下内容,并检查这是否解决了您的问题:

onSubmit: async (formValues) => {
          console.log('submit', formValues);
          setSubmitting(true);
          try {
              const res = await api('api/auth/register', {
                  method:'POST',
                  body: JSON.stringify(formValues)
              });
              if(!res.ok){
                  const er = res[0].message;       // when I have errror and response error message
                  console.log('Error',er);          // display response error message
              } else {
              const token = res.token.token;   // when the user is unique and there is no error
              localStorage.setItem('myToken', token);

              console.log('Result!',token);     //display in console token
              history.push("/home");
             }
          } catch(e) {
              console.error(e);
          } finally {
              setSubmitting(false);
          }   
      },  
    });


欢迎来到SO!您没有提供足够的代码,我们无法告诉您如何解决问题。例如,什么是
api
函数?如果是400错误,您将在catch
catch(e){console.error(e);}
中看到错误消息code@TapanDave对于
fetch
API AFAIK,事实并非如此,它不会引发错误响应。您需要检查
response.ok
response.status
以查看请求是否失败。例如,它仅在您脱机时抛出。如果响应为错误,您是否仍有该
令牌
?如果没有,那么您在尝试访问
res.token.token
中未定义的
令牌时会出现错误。欢迎使用堆栈溢出!请不要污损你的帖子。通过在Stack Exchange网络上发布,您已授予SE分发该内容的不可撤销的权利(根据)。根据SE政策,任何故意破坏行为都将被恢复。您是否仍存在屏幕截图中描述的
未定义的
错误?
export const api = async (url, args) => { 
  const response = await fetch(`${apiUrl}${url}`, {
   ...args,
    headers: {
      "Content-type": "application/json; charset=UTF-8 " ,
      "Accept": 'application/json',
      ...args.headers,  
    },
  });

 return response.json();    

}

onSubmit: async (formValues) => {
          console.log('submit', formValues);
          setSubmitting(true);
          try {
              const res = await api('api/auth/register', {
                  method:'POST',
                  body: JSON.stringify(formValues)
              });
              if(!res.ok){
                  const er = res[0].message;       // when I have errror and response error message
                  console.log('Error',er);          // display response error message
              } else {
              const token = res.token.token;   // when the user is unique and there is no error
              localStorage.setItem('myToken', token);

              console.log('Result!',token);     //display in console token
              history.push("/home");
             }
          } catch(e) {
              console.error(e);
          } finally {
              setSubmitting(false);
          }   
      },  
    });