Axios post request: Add data to variable

Hi All

I make a post request with axios:

  data:{
    search: '',
    skills: [],
    skillName: '',
  },

  methods:{
    addSkill: function(){
      axios.post('skills',{
        skillName: this.skillName
      }).then((response)=>(this.skills = response.data));
    },
  },

When the post request was sucessfully the server sends back the list with the skills, which it sets to the skills variable (then((response)=>(this.skills = response.data))).
But I would also like to do some other thins there, z.B. set back the skillName to ‘’. How I need to do this?

something like this:

.then(function(response){
 this.skills = response.data;
 this.skillName = '';
});

But this is not working.

Thank you very much!

Best regards
Simoon

I would suggest making a separate method to handle the response so you could reuse for edit also but anyways it would also keep the code cleaner than trying to jam it all in after the response.

axios.post('skills',{
      skillName: this.skillName
   })
   .then(response => {
      this.handleResponse(response)
   })
   .catch(e => {
      this.handleErrors(e)
   })

and simple handleResponse method:

handleResponse(response){
   // do all your modifications or setting data to whatever in here
   this.skills = response.data;
   this.skillName = '';
}

But yes basically on the right track

Thank you very much! This solved my problem. :grinning: