gpt4 book ai didi

How to clear the console from the 404 error, which you did by fetch?(如何清除控制台的404错误,你做的fetch?)

转载 作者:bug小助手 更新时间:2023-10-26 20:06:09 24 4
gpt4 key购买 nike



I needed to to check some URL by fetch if it exist, and rest is some error in console

我需要通过FETCH来检查一些URL是否存在,而REST在控制台中出现了一些错误


  useEffect(()=>{
for(let item of data){
fetch(`variable data by item`)
.then((res) => res.status)
.then(res =>{ if(res ===200){
item.hasUrl = true;
item.urlImage = `var data`
}else{
item.hasUrl = false;
} })
}
},[data])

So I need a method to clear the console.

因此,我需要一种方法来清除控制台。


I try to clear it by:

我试着通过以下方式来清除它:


console.clear()

and if it works, I don't want the message:

如果它起作用了,我不想要这样的信息:



"Console data has been erased"



Anyone have another method to do that?

有谁有其他方法可以做到这一点?


更多回答

Catch the error and do nothing

抓住错误,什么都不做

I think the browser's own logging of network activity (like 404 errors) isn't something we can control directly from JavaScript code due to security and transparency reasons.

出于安全性和透明性的原因,我认为浏览器自己对网络活动的记录(如404错误)不是我们可以从JavaScript代码直接控制的。

why are you trying to hide an unsuccessful fetch? Are you doing something malicious? If not, then stop worrying about the console

为什么你要试图隐藏一次不成功的取球?你在做什么恶意的事吗?如果不是,那么就不要再担心控制台了

If you truly want to hide the request result, make the request in such a way that it always "succeeds" - i.e. through your own server for example, always returning a 200 status, but the data returned determines your codes next actions

如果您真的想要隐藏请求结果,请以一种使其始终“成功”的方式发出请求--例如,通过您自己的服务器,始终返回200状态,但返回的数据决定您的代码下一步操作

Also, on a JS note: don't use the same name for different things. fetch(url).then(res => res.status).then(status => ...), not fetch(url).then(res => res.status).then(res => ...). That's just asking for future bugs (or headaches at the very least). Also, your for is synchronous but fetch is async, so that's something else you'll want to fix. Either use a Promise.all or use an awaiting foreach (and really: use a Promise.all)

另外,JS注意:不要对不同的事物使用相同的名称。Fetch(Url).Then(res=>res.atus).Then(Status=>...),Not Fetch(Url).Then(res=>res.atus).Then(res=>...).这只是在要求将来出现错误(或者至少是令人头疼的问题)。此外,您的for是同步的,但FETCH是异步的,所以这是您想要修复的其他问题。要么使用Promise.all,要么使用等待的Foreach(说真的:使用Promise.all)

优秀答案推荐

If you mean a method that clears the JavaScript console and control the browsers output message, you can't. Developers don't have access to the browser internal messages.

如果您指的是清除JavaScript控制台并控制浏览器输出消息的方法,那么您就不能这样做。开发人员无权访问浏览器内部消息。


更多回答

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