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
Add a ment  | 

3 Answers 3

Reset to default 3

It 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