gpt4 book ai didi

reactjs - 在 React js 中获取时如何隐藏控制台状态错误消息?

转载 作者:行者123 更新时间:2023-12-03 22:13:14 30 4
gpt4 key购买 nike

在我的 React 应用程序中,我使用的是 fetch()从我的 API 获取数据,_callAPI()函数获取 domain如果我的数据库中存在域的网站,则参数并调用 API。如果存在,则返回网站的对象,否则返回 500。因此,在使用 fetch() 之前,我无法确定该网站是否存在。问题是每次 fetch() 都找不到任何东西时,它会抛出以下内容:

container.jsx:25 GET http://localhost:3000/boutiques/detail/?q=testdomain.com 500 (Internal Server Error)



当它没有找到很多网站时,控制台日志中会包含该错误消息。有没有办法在获取时忽略那种消息?

fetch()
_callApi = () => {
const { domain } = this.props;

return fetch(`/boutiques/detail/?q=${domain}`)
.then(response => {
if (response.status === 500) {
return 500;
}
return response.json();
})
.then(json => json)
.catch(err => console.log(err));
};

最佳答案

如果您想静音浏览器错误:

Unfortunately, this cannot be done, as this type of message in the console is printed by chrome itself. Repressing this type of message has been debated for years, but the consensus seems to be that this message is desirable



Credits .

如果您想静音控制台中的 Unhandled 错误 :

您始终可以在前端消除错误,如下所示:
.catch(err => { const mute = err })

但是最好以某种方式通知用户有关错误的信息,而不是执行此类解决方法。

此外,您的服务器最好在响应中返回错误消息,并且在前端您将继续执行它。

查看您的情况,服务器以状态代码 400 进行响应可能会更好。以下是 HTTP 错误代码及其用途:
  • 4xx (客户端错误):请求包含错误的语法或不能
    已完成
  • 5xx (服务器错误):服务器未能完成
    显然有效的请求
  • 关于reactjs - 在 React js 中获取时如何隐藏控制台状态错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52807184/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com