admin管理员组文章数量:1245030
Here's my code:
async [types.GET_DATA]({mit, state}, data) {
try {
const res = await axios.post('/login', {
email: data.email,
password: data.password,
});
console.log(res)
} catch(e) {
if(e.response) {
console.log(e.response)
}
}
}
So, I return 400 Bad Request whenever user sends empty fields. What axios does is throws the error along with the error response. What I need to do is remove that console error message and only get the error response.
How can I do it?
Here's my code:
async [types.GET_DATA]({mit, state}, data) {
try {
const res = await axios.post('/login', {
email: data.email,
password: data.password,
});
console.log(res)
} catch(e) {
if(e.response) {
console.log(e.response)
}
}
}
So, I return 400 Bad Request whenever user sends empty fields. What axios does is throws the error along with the error response. What I need to do is remove that console error message and only get the error response.
How can I do it?
Share Improve this question asked Aug 23, 2018 at 5:40 AxelAxel 5,13118 gold badges76 silver badges148 bronze badges 5- While you could, I would think it might be more elegant to check the fields beforehand, and only send out the request if they're non-empty? Just an idea – CertainPerformance Commented Aug 23, 2018 at 5:42
- Well, that's cool but I prefer backend validation – Axel Commented Aug 23, 2018 at 5:43
- so in this case use a promise based approch – Constantin Guidon Commented Aug 23, 2018 at 5:43
- async/await is there already :) – Axel Commented Aug 23, 2018 at 5:44
- 2 You should always validate in both frontend and backend – dziraf Commented Aug 23, 2018 at 7:06
3 Answers
Reset to default 3It is actually impossible to do with JavaScript. This is due to security concerns and a potential for a script to hide its activity from the user.
The best you can do is hide them from only your console.
async [types.GET_DATA]({mit, state}, data) {
try {
const res = await axios.post('/login', {
email: data.email,
password: data.password
});
console.log(res)
} catch(err) {
console.log(err)
}
}
If there is an error you can catch it like this:
axios.get("https://google.").then(response => {
console.log("Done");
}).catch(err => {
console.log("Error");
})
本文标签: javascriptHow to remove the console errors in axiosStack Overflow
版权声明:本文标题:javascript - How to remove the console errors in axios? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740243430a2247796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论