【发布时间】:2019-03-19 07:37:26
【问题描述】:
在我的 React 应用程序中,我使用 fetch() 从我的 API 获取数据,_callAPI() 函数获取 domain 参数并在我的数据库中存在该域的网站时调用 API。如果存在,则返回网站的对象,否则返回 500。因此,在使用 fetch() 之前,我无法确定网站是否存在。问题是每次 fetch() 没有找到任何东西时,它都会抛出以下内容:
container.jsx:25 获取 http://localhost:3000/boutiques/detail/?q=testdomain.com 500(内部服务器错误)
当它没有找到很多网站时,控制台日志会包含该错误消息。有没有办法在获取时忽略这种消息?
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));
};
【问题讨论】: