Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "'response' is defined but never used" on axios.then callback

Tags:

eslint

axios

I'm submitting a Post request using Axios, and I want to give the user some confirmation if the request is successful. However, I have no use for the response variable, so I get an eslint error. How to fix this?

axios.post('/api/option.json', {
    choices: myChoices
  })
  .then(response => {
    alert('Form was submitted successfully')
  })

Error:

Module Error (from ./node_modules/eslint-loader/index.js):
error: 'response' is defined but never used (no-unused-vars) at src/components/Options.vue:78:15

EDIT (April 2020): Oops, looks like this question now has 1k views but 0 upvotes. I'm guessing I wrote a tantalizing title but the question isn't helping people. Please comment if I should rename this or link to a better question?

like image 217
krubo Avatar asked Oct 19 '19 18:10

krubo


1 Answers

'variable' is defined but never used, this error simply means the variable that you declared is not used in the program.

Solution -

In your program use response as the return value.

axios.post("/api/option.json", {
  choices: myChoices;
})
.then(response => {
  alert("Form was submitted successfully");
  return response
});

OR

axios.post("/api/option.json", {
  choices: myChoices;
})
.then(() => {
  alert("Form was submitted successfully");
});

OR

If you do not like this feature of eslint you can turn off by adding this object in package.json file.

"eslintConfig": {
    "rules": {
      "no-console": "off",
      "no-unused-vars": "off"
    }
},
like image 93
Rohit Nishad Avatar answered Oct 12 '22 22:10

Rohit Nishad